mercredi 28 avril 2021

assigning an object to C++ array

I am examining the following code in C++:


#include <iostream>

using namespace std;

class Person{
    int age;
    Person(int age){
        this->age = age;
    }
};

int main()
{
    Person a = Person(2);
    Person temp[] = {a};
    temp[0].age = 5;
    cout << temp[0].age;
    return 0;
}

So my guess is when one assigns an object to a slot of an array in C++, that is equivalent to copying that object into the array. Thus when we change that element in the array, it will not affect the original object. Is that correct?

Aucun commentaire:

Enregistrer un commentaire