[英]C++ copy constructor with object pointer as argument
对于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;
和b
是深拷贝a
。 现在的问题是我想要写A* b = new A(a);
那么,如何指定一个构造函数,该构造函数使用指向A
的指针并在堆上创建一个新的A
?
哦,好吧,脑子屁……我刚刚意识到我自己可以提供一个构造函数A::A(const A* other)
而无需使用复制/分配构造函数,或者只写A* b = new A(*a);
如评论中所建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.