为什么复制构造函数不被“链接”?像默认的构造函数和析构函数? [英] Why aren't copy constructors "chained" like default constructors and destructors?

查看:82
本文介绍了为什么复制构造函数不被“链接”?像默认的构造函数和析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不链接复制构造函数(如默认ctor或dtor),以便在调用派生类的复制构造函数之前,先调用基类的复制构造函数?使用默认构造函数和析构函数,它们分别在从基础到派生和在派生到基础的链中被调用。为什么复制构造函数不是这种情况?例如,这段代码:

Why aren't copy constructors chained (like default ctors or dtors) so that before the derived class's copy constructor is called, the base class's copy constructor is called? With default constructors and destructors, they are called in a chain from base-to-derived and derived-to-base, respectively. Why isn't this the case for copy constructors? For example, this code:

class Base {
public:
    Base() : basedata(rand()) { }

    Base(const Base& src) : basedata(src.basedata) {
        cout << "Base::Base(const Base&)" << endl;
    }

    void printdata() {
        cout << basedata << endl;
    }

private:
    int basedata;
};

class Derived : public Base {
public:
    Derived() { }

    Derived(const Derived& d) {
        cout << "Derived::Derived(const Derived&)" << endl;
    }
};


srand(time(0));


Derived d1;      // basedata is initialised to rand() thanks to Base::Base()

d1.printdata();  // prints the random number

Derived d2 = d1; // basedata is initialised to rand() again from Base::Base()
                 // Derived::Derived(const Derived&) is called but not
                 // Base::Base(const Base&)

d2.printdata();  // prints a different random number

复制构造函数实际上不能(不能)对象的副本,因为 Derived :: Derived(const Derived&)无法访问 basedata 进行更改。

The copy constructor doesn't (can't) really make a copy of the object because Derived::Derived(const Derived&) can't access basedata to change it.

关于复制构造函数,我是否缺少一些基本知识,以至于我的思维模式不正确,或者这种设计是否存在某些不可思议的原因? p>

Is there something fundamental I'm missing about copy constructors so that my mental model is incorrect, or is there some arcane (or not arcane) reason for this design?

推荐答案


复制构造函数实际上不能(不能)复制对象,因为 Derived :: Derived(const Derived&)无法访问 pdata 进行更改。

确定可以:

Derived(const Derived& d)
    : Base(d)
{
    cout << "Derived::Derived(const B&)" << endl;
}

如果您未在初始化列表中指定基类构造函数,默认构造函数被调用。如果要调用默认构造函数以外的其他构造函数,则必须指定要调用的构造函数(以及带有哪些参数)。

If you don't specify a base class constructor in the initializer list, its default constructor is called. If you want a constructor other than the default constructor to be called, you must specify which constructor (and with which arguments) you want to call.

相同为什么是这样:为什么复制构造函数与任何其他构造函数应该有所不同?作为一个实际问题的示例:

As for why this is the case: why should a copy constructor be any different from any other constructor? As an example of a practical problem:

struct Base
{
    Base() { }
    Base(Base volatile&) { } // (1)
    Base(Base const&)    { } // (2)
};

struct Derived : Base
{
    Derived(Derived&) { }
};

Base 复制构造函数中的哪个期望 Derived 复制构造函数调用?

Which of the Base copy constructors would you expect the Derived copy constructor to call?

这篇关于为什么复制构造函数不被“链接”?像默认的构造函数和析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆