c++ - C++ 中的提升构造函数

标签 c++ constructor

什么是推广构造函数?它与复制构造函数/赋值运算符有什么关系吗?我见过一个例子,但无法理解。

#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/

相关文章:

c++ - 为什么默认使用 4.8 时 b2 使用 gcc4.6 构建?

c++ - 带指针的 Foreach 循环

C# 对象初始化选项

testing - ES6 构造函数可以更容易地用 Sinon stub 吗?

java - 如果使用相同的值构造,则使用现有实例

c++ - Windows Mobile WAP 唤醒

c++ - 如何优雅地声明变量集的子集

c++ - 如何同步 Lua 和 C++ 垃圾回收

c++ - C++0x 中的特殊成员函数

constructor - Constexpr 隐式声明的函数