假设我们有一个类A,它包含作为同一类的成员:
Say we have a class A that contains as a member of the same class:
Class A{ const A &a; }我想创建一个参数化的构造函数,我不想定义类的复制构造函数。
I want to create a parametized constructor that passed the value of that member, but I do not want to define the copy constructor of the class.
A(const A& memberA): a(memberA) {}如何表示编译器这样的东西?
How could indicate the compiler such thing?
感谢
推荐答案一个构造函数只需要引用它构造的类一个复制构造函数,无论你想要它是一个或没有。复制构造函数定义如下:
A constructor that can take just a reference to the class it constructs is a copy constructor, whether you want it to be one or not. Copy constructors are defined thus:
class X 的非模板构造函数复制构造函数,如果其第一个参数是 X& , const X& , volatile X& 或 const volatile X& ,并且没有其他参数或所有其他参数都有默认参数。
A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.
你可以声明它 explicit 来限制类的复制(防止 A a = A()),但它仍然是一个复制构造函数,只要它有该签名。
You could declare it explicit to restrict how the class can be copied (preventing A a = A() for example), but it's still a copy constructor as long as it has that signature.
更多推荐
参数化构造函数不是复制一个
发布评论