C++复制构造函数

发布于 2024-10-30 20:18:08 字数 350 浏览 1 评论 0原文

我有下一个问题: 如果我有一个包含实例的类

Class A {  
public:  
    A();  
    ~A();  
    ...  
protected:  
    //Data  
    std::string str;  
    std::vector<char> vctChar;  
    std::vector<std::string> vctString;  
}  

,并且我使用默认的 C.Ctor 和赋值。假设我知道声明该对象并将其用于另一个对象作为 C.Ctor,新对象是否将拥有自己的数据副本(字符串和向量?),或者它将指向第一个对象的同一个对象?

I have the next question:
If I have a class which contains for instance

Class A {  
public:  
    A();  
    ~A();  
    ...  
protected:  
    //Data  
    std::string str;  
    std::vector<char> vctChar;  
    std::vector<std::string> vctString;  
}  

and I use the DEFAULT C.Ctor and assignement. Suppose I know declare the object and use it for another object as C.Ctor, will the new object will have it own copy of data (string and vectors ?) or it will point to the same one of the first object ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半仙 2024-11-06 20:18:08

我使用默认的 C.Ctor 和
作业。假设我知道声明
该对象并将其用于另一个对象
对象作为 C.Ctor,将新对象
将拥有自己的数据副本(字符串
和向量

如果您的类没有指针,则编译器生成的默认复制构造函数和operator= 将对您的成员变量进行(有效)深度复制。所以,是的,新对象(使用复制构造函数或运算符=创建)将拥有自己的数据副本(字符串和向量)。

但是,如果您的类 A 有一个类型为 struct X 的成员,该成员具有指针字段,那么您需要定义复制构造函数和 operator= 对于此结构X,以便类A 的默认复制构造函数和operator= 将产生您期望的所需结果。

I use the DEFAULT C.Ctor and
assignement. Suppose I know declare
the object and use it for another
object as C.Ctor, will the new object
will have it own copy of data (string
and vectors

If your class doesn't has pointers, then the default copy-constructor and operator= generated by the compiler would make deep-copy (effectively) of your member variables. So yes, the new objects (created using either copy-constructor or operator=) will have their own copy of data (string, and vectors).

However, if your class A has a member of type say struct X which has pointer fields, then you need to define copy-constructor and operator= for this struct X so that the default copy-constructor and operator= for class A would produce desired result as you expect.

娇纵 2024-11-06 20:18:08

它将拥有自己的 stringvector 成员副本(尽管在内部,字符串可能仍然引用相同的 char 数组,只要它们都没有更改 -但这没关系)。

对于非原始成员,默认的复制构造函数和赋值运算符调用各自的复制构造函数/赋值运算符,这对于标准库类来说效果很好。指针会出现问题,默认情况下只是复制它们的值,这会导致两个指针引用同一地址。

It will have its own copies of the string and vector members (although internally, the strings may still refer to the same char array, as long as none of them are changed - but this is OK).

For nonprimitive members, the default copy constructor and assignment operator calls their respective copy constructor / assignment operator, which works just fine for standard library classes. The problem arises with pointers, where the default is to just copy their value, which results in two pointers referencing the same address.

天煞孤星 2024-11-06 20:18:08

每个人都有自己的、单独的数据副本。

如果您想要副本共享相同的数据,请阅读boost::shared_ptr<>(如果您的编译器附带,则使用 std::shared_ptr<> 代替)。

Each will have their own, separate copy of the data.

If you want copies to share the same data, read up on boost::shared_ptr<> (then use std::shared_ptr<> instead if your compiler ships with it).

霓裳挽歌倾城醉 2024-11-06 20:18:08

是的。合成的复制构造函数调用所有成员的复制构造函数,并且 std::string/std::vector 也具有可以正确复制所有元素的复制构造函数。

对于被指针隐藏的原始数组来说,情况并非如此,它不会自动执行深层复制。

Yes. The synthesised copy constructor invokes the copy constructor of all members, and std::string/std::vector have copy constructors that properly copy all the elements too.

This is not the case for raw arrays hidden by pointers, which would not perform the deep copy automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文