调用复制构造函数的逻辑
class base {
public:
base(){
cout << "base constructor" << endl;
}
base(const base& rh) {
cout << "base copy constructor" << endl;
}
};
//case 1:
class der : public base {
};
//case 2:
class der : public base {
public:
der(){
cout << "der constructor" << endl;
}
der(const der& rh) {
cout << "der copy constructor" << endl;
}
};
int main() {
der d;
der d1(d);
}
情况1:der d1(d);调用基类复制构造函数,而在
case-2,基类默认构造函数和der类复制构造函数 被调用。
谁能解释一下其中的逻辑吗?
class base {
public:
base(){
cout << "base constructor" << endl;
}
base(const base& rh) {
cout << "base copy constructor" << endl;
}
};
//case 1:
class der : public base {
};
//case 2:
class der : public base {
public:
der(){
cout << "der constructor" << endl;
}
der(const der& rh) {
cout << "der copy constructor" << endl;
}
};
int main() {
der d;
der d1(d);
}
case 1: der d1(d); invokes base class copy constructor whereas in
case-2, base class default constructor and der class copy constructor
is invoked.
Can anyone explain the logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在情况 1 中,您将获得编译器合成的默认复制构造函数。这被定义为复制基数和成员。
在情况 2 中,您定义了自己的复制构造函数,它执行您告诉它执行的操作。您没有在基类的初始值设定项列表中放置任何内容,因此基类是默认构造的[*],与任何其他未显式初始化基类的构造函数相同。如果
der
有任何数据成员,这些也不会被复制。[*] 或其他类型的初始化之一,对于非 POD 类来说相当于相同的事情。我永远记不起那些细节。
In case 1, you get the default copy constructor synthesized by the compiler. This is defined to copy bases and members.
In case 2, you defined your own copy constructor, which does what you tell it to do. You didn't put anything in the initializer list for the base class, so the base is default-constructed[*], same as any other constructor that doesn't explicitly initialize the base. If
der
had any data members, those would not be copied either.[*] or one of the other kinds of initialization that amounts to the same thing for non-POD classes. I can never remember those details.
默认情况下,派生复制构造函数不会调用基类复制构造函数本身。当你不告诉派生类复制构造函数调用基类复制构造函数时,它仍然需要构造基类子对象,因此它必须调用基类默认构造函数。
但在下面的示例中,您将看到可以在派生类的成员初始化列表中显式添加对基复制构造函数的调用:
Derived copy constructor will not call base class copy constructor itself by default. When you don't tell the derived class copy constructor to call the base class copy constructor , it will still need to construct the base sub-object, so it will have to call the base default constructor.
But in the example below you , you will see you can add calling to base copy constructor in the member initilize list explicitly of derived class:
在第一种情况下,您没有为
der
指定构造函数,因此编译器会为您决定,即将d
的数据复制到d1
单独地,在进程中调用base
的复制构造函数。在第二种情况下,您指定了复制构造函数,其中您自己不调用
base
的复制构造函数。由于您已经重写了 der 的复制构造函数(它告诉编译器执行您所说的操作,而不是执行其想要的操作),因此编译器无法假设您打算调用复制构造函数base
的构造函数也是如此。In the first case, you are not specifying constructors for
der
, therefore the compiler decides for you, that is copy the data ofd
tod1
individually, invoking copy constructor ofbase
in the process.In the second case, you have specified the copy constructor, in which you do not invoke the copy constructor of
base
yourself. Since you have overridden the copy constructor ofder
(which tells the compiler to do what you say, not what it wants), the compiler cannot assume you intended to invoke the copy constructor ofbase
too.基本上是对一个复制者的调用。因此,在 case-2 中,将调用 der 的 copy-con,如果您愿意,可以在代码中使用 base 的 copy-con。在第一种情况下,由于您没有在 der 中实现 copy-con,它会从其基类中调用该函数,就像处理仅在基类中实现的任何函数一样。
Basically there is a call to one copy-con. So in case-2 the copy-con of der is called, and if you like you can use the copy-con of the base in the code. In case one, since you didn't implemented the copy-con in der it calls the function from his base, like it would do with any function that is only implemented in a base class.