[英]C++ class constructor using a pointer to same class object
Does it make sense to have class constuctor which takes a pointer to a object of the same class. The idea is to copy all the data for from the old object to a new object.拥有一个 class 构造函数是否有意义,它采用指向相同 class 的 object 的指针。这个想法是将旧 object 的所有数据复制到新的 object。
class Shape {
...
public:
Shape(string,string,...)
Shape(Shape*)
}
Shape::Shape(Shape* ref) {
layer = ref->layer;
purpose = ref->purpose;
...
}
No, this will only lead to confusing ambiguities and hard-to-catch errors (automatic conversion from pointer to object, anyone?).不,这只会导致令人困惑的歧义和难以捕捉的错误(从指针自动转换为 object,有人吗?)。 If initializing from a pointer make it explicit:如果从指针初始化使其显式:
Shape s2 = *s1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.