调用复制构造函数的逻辑

发布于 2024-12-04 06:57:03 字数 645 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纸短情长 2024-12-11 06:57:03

在情况 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.

咆哮 2024-12-11 06:57:03

默认情况下,派生复制构造函数不会调用基类复制构造函数本身。当你不告诉派生类复制构造函数调用基类复制构造函数时,它仍然需要构造基类子对象,因此它必须调用基类默认构造函数。

但在下面的示例中,您将看到可以在派生类的成员初始化列表中显式添加对基复制构造函数的调用:

class base {
public:
    base(int i):m_i(i){
        cout << "base constructor" << endl;
    }
    base(const base& rh) {
        m_i = rh.m_i;
        cout << "base copy constructor" << endl;
    }
private:
int m_i;
};

//case 2: 
class der : public base {
public:
    der(int i,int j):base(i),m_j(j){
        cout << "der constructor" << endl;
    }
    der(const der& rh):base(rh) {
        m_j = rh.m_j;
        cout << "der copy constructor" << endl;
    }
private:
    int m_j;
};

int main() {
 der d(1,2);
 der d1(d); //d1.m_i = 1, d1.m_j = 2
}

//result
//base copy constructor
//der copy constructor

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:

class base {
public:
    base(int i):m_i(i){
        cout << "base constructor" << endl;
    }
    base(const base& rh) {
        m_i = rh.m_i;
        cout << "base copy constructor" << endl;
    }
private:
int m_i;
};

//case 2: 
class der : public base {
public:
    der(int i,int j):base(i),m_j(j){
        cout << "der constructor" << endl;
    }
    der(const der& rh):base(rh) {
        m_j = rh.m_j;
        cout << "der copy constructor" << endl;
    }
private:
    int m_j;
};

int main() {
 der d(1,2);
 der d1(d); //d1.m_i = 1, d1.m_j = 2
}

//result
//base copy constructor
//der copy constructor
过期以后 2024-12-11 06:57:03

在第一种情况下,您没有为 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 of d to d1 individually, invoking copy constructor of base 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 of der (which tells the compiler to do what you say, not what it wants), the compiler cannot assume you intended to invoke the copy constructor of base too.

野心澎湃 2024-12-11 06:57:03

基本上是对一个复制者的调用。因此,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文