Why aren#39;t copy constructors quot;chainedquot; like default constructors and destructors?(为什么复制构造函数不“链接?比如默认构造函数和析构函数?)
问题描述
为什么不链接复制构造函数(如默认构造函数或 dtors),以便在调用派生类的复制构造函数之前调用基类的复制构造函数?对于默认构造函数和析构函数,它们分别在从基到派生和派生到基的链中调用.为什么复制构造函数不是这种情况?例如,这段代码:
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.
我是否遗漏了一些关于复制构造函数的基本原理,以至于我的心智模型不正确,或者这种设计是否有一些神秘(或不神秘)的原因?
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&) { }
};
您希望 Derived
复制构造函数调用哪些 Base
复制构造函数?
Which of the Base
copy constructors would you expect the Derived
copy constructor to call?
这篇关于为什么复制构造函数不“链接"?比如默认构造函数和析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么复制构造函数不“链接"?比如默认构造函数和析构函数?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07