c+中的按引用调用或按值调用+; 我是C++初学者,通过引用或价值理解了基本的呼叫理念。但是在下面的例子中,我搞不清楚这个调用是通过引用还是通过值。我正在阅读其他人的代码,我通过剪切其他逻辑简化了它,只保留显示它是通过引用调用还是通过值调用的逻辑

c+中的按引用调用或按值调用+; 我是C++初学者,通过引用或价值理解了基本的呼叫理念。但是在下面的例子中,我搞不清楚这个调用是通过引用还是通过值。我正在阅读其他人的代码,我通过剪切其他逻辑简化了它,只保留显示它是通过引用调用还是通过值调用的逻辑,c++,C++,这是密码 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() {

这是密码

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
}
我的问题是为什么a.theProfile与测试不一样?根据我的理解,profile是(*aProflie)的引用,而aProfile是指向测试对象的指针,这意味着它们共享同一个地址。如果此地址中的test更改,为什么test.test不更改为2


有人能帮我理解这一点吗?谢谢

是指向调用对象的指针。因此,为了分配给对象,必须取消引用
指针。
资产
构造函数按值获取配置文件指针,当它被取消引用以传递给
配置文件
构造函数时,被指向的对象将通过引用传递

是指向调用对象的指针。因此,为了分配给对象,必须取消引用
指针。
资产
构造函数按值获取配置文件指针,当它被取消引用以传递给
配置文件
构造函数时,被指向的对象将通过引用传递

“据我所知,该文件是参考文件”为什么?它被声明为
Profile文件,因此-它不是引用。如果它被声明为
配置文件&Profile-这将是一个参考。对于第一个问题:这是否回答了您的问题?请不要同时问多个无关的问题。您可以创建多个问题帖子。@AlgirdasPreidžius,明白了,thanks@walnut是的,它回答了我的第一个问题。谢谢。“据我所知,该文件是参考文件”为什么?它被声明为
Profile文件,因此-它不是引用。如果它被声明为
配置文件&Profile-这将是一个参考。对于第一个问题:这是否回答了您的问题?请不要同时问多个无关的问题。您可以创建多个问题帖子。@AlgirdasPreidžius,明白了,thanks@walnut是的,它回答了我的第一个问题。谢谢