我有这个功能

int getrelation(string name, RELATION& output){

        bool found=0;
        int index=0;
        for(int i=0;i<a_attributes.size();i++){
            if(name==a_attributes[i].str_name){
                found=1;
                index=i;
            }
        }
        if(!found){
            printf("relation not found");
            return 1;
        }
        output=a_attributes[index];

        return 0;
    }

RELATION是一门课
a_attributes是关系的 vector 。

它应该返回对关系对象的引用。调用getrelation()后,如果我更改输出的值,那么也应该更改a_attributes[index]的值,因为这是一个浅拷贝,对吗?

最佳答案

这实际上取决于您的赋值运算符,此处未列出。

线
output=a_attributes[index];
将使用赋值运算符设置输出。如果该赋值运算符进行了深拷贝,那么您将获得一个深拷贝。

关于c++ - C++浅/深复制?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7533686/

10-11 22:24
查看更多