Don*_*nut -1 c++ copy-constructor
我有一个A类.在这个类中它包含一个指向另一个A的指针.
class A
{
A* sub = NULL;
};
Run Code Online (Sandbox Code Playgroud)
我想有一个默认指针的空构造函数NULL,以及另一个传递指针/引用的构造函数.第二个构造函数将参数复制到一个new A()对象中,并将sub从参数传递给它自己.
现在的班级:
class A
{
A* sub = NULL
A(A* source)
{
this->sub = new A(*source);//copy the source 'A'
// we now have a copy of "source" and all of its children
// but to prevent the "source" from deleting our new
// children (destructor deletes children recursively),
// "source"s children are disconnected from "source"
source->sub = NULL;
// this invalidates sub, but that is desired for my class
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这没有问题.相反,问题是我希望将"source"变量作为参考.这是一个问题,因为这会使构造函数具有复制构造函数的签名.
有没有办法告诉编译器这不应该被认为是复制构造函数?如果可能,甚至可以这样做?