I am a C++ beginner and I understand basic idea of call by reference or value. But in the following case, I got confused whether this call by reference or value. I am reading other people's code, and I simplified it by cutting other logic, just keep the logic which shows whether it call by reference or value.
Here is the code
class Profile
{
public:
int test = 1;
Profile() {}
Profile(const Profile& original) // create a reference original and then assign the object (*aProfile) to it
{
*this = original;
}
void change()
{
test = 2;
}
};
class Asset
{
public:
Profile theProfile;
Asset() {}
Asset(Profile *aProfile) // aProfile should be a pointer points to Profile
{
theProfile = Profile(*aProfile); // create another object by (*aProfile)
theProfile.change();
}
};
int main() {
Profile test; // create the object test
Asset a(&test); // take the address of test as argument
}
And my question is why the a.theProfile is not the same as the test? From my understanding theProfile is the reference of the (*aProflie) and aProfile is pointer points to test object, which means they share one same address. If test in this address changed, why didn't test.test change to 2?
Could anyone help me to understand this? thanks!