用对象指针作为参数的C++复制构造函数

C++ copy constructor with object pointer as argument

本文关键字:C++ 复制 构造函数 参数 对象 指针      更新时间:2023-10-16

我对C++的复制和赋值构造函数仍然有点不确定。到目前为止,我所拥有的是A.hpp:

class A {
private:
     char* str;
public:
     A(char* str);
     // strcpy str from other to this.
     A(const A& other);
     // free str in this, and strcpy str from other to this.
     A& operator=(const Type& other);
}

假设我有A* a = new A(some_char_str);,我能写A b = *a;,而ba的深度复制。现在的问题是,我想要写A* b = new A(a);的能力。那么,我如何指定一个构造函数来获取指向A的指针,并在堆上创建一个新的A呢?

哦,好吧,大脑放屁了。。。我刚刚意识到,我可以自己提供一个构造函数CCD_,或者只是按照注释中的建议编写CCD_ 10。