什么是推广构造函数?它与复制构造函数/赋值运算符有什么关系吗?我见过一个例子,但无法理解。
#include <iostream>
#include <vector>
using namespace std;
class base{
public:
base(){
cout << endl << "base class default construtor" << endl;
}
base(const base& ref1){
cout << endl << "base class copy constructor" << endl;
}
base(int) {
cout << endl << "base class promotion constructor" << endl;
}
};
class derived : public base{
public:
derived(){
cout << endl << "derived class default constructor" << endl;
}
derived(const derived& ref2){
cout << endl << "derived class copy constructor" << endl;
}
derived(int) {
cout << endl << "derived class promotion constructor" << endl;
}
};
int main(){
vector<base> vect;
vect.push_back(base(1));
vect.push_back(base(1));
vect.push_back(base(2));
return 0;
}
当我编译和执行时:比命令是这样的:
基类提升构造函数
基类拷贝构造函数
基类提升构造函数
基类提升构造函数
基类拷贝构造函数
基类提升构造函数
基类提升构造函数
基类拷贝构造函数
基类提升构造函数
请帮助我理解提升构造函数的概念。我在网上搜索过,但没有得到太多这方面的信息。
谢谢
最佳答案
我第一次听说“促销构造器”这个词。在您的代码中以这种方式命名的两个构造函数都符合 converting constructor 的定义.
关于c++ - C++ 中的提升构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21305689/