构造函数 (C++)

如果未定义任何构造函数,则编译器会生成不采用任何参数的默认构造函数;可以通过将默认构造函数声明为已删除来重写此行为。

构造函数顺序

  • 按声明顺序调用基类和成员构造函数。

  • 如果类派生自虚拟基类,则会将对象的虚拟基指针初始化。

  • 虚函数指针指向类中的虚函数表,确保虚函数正确地调用绑定代码。

  • 它执行自己函数体中的所有代码。

  • C++

    #include <iostream> using namespace std; class Contained1 { public: Contained1() { cout << "Contained1 constructor." << endl; } }; class Contained2 { public: Contained2() { cout << "Contained2 constructor." << endl; } }; class Contained3 { public: Contained3() { cout << "Contained3 constructor." << endl; } }; class BaseContainer { public: BaseContainer() { cout << "BaseContainer constructor." << endl; } private: Contained1 c1; Contained2 c2; }; class DerivedContainer : public BaseContainer { public: DerivedContainer() : BaseContainer() { cout << "DerivedContainer constructor." << endl; } private: Contained3 c3; }; int main() { DerivedContainer dc; int x = 3; }

    这是输出:

    Contained1 constructor.
    Contained2 constructor.
    BaseContainer constructor.
    Contained3 constructor.
    DerivedContainer constructor.
    

    如果构造函数引发异常,析构的顺序与构造的顺序相反:

    1. 构造函数主体中的代码将展开。

    2. 基类和成员对象将被销毁,顺序与声明顺序相反。

    3. 但是,对象本身不是完全构造的,因此析构函数不会运行。

    成员列表

    此方法使用直接初始化,这比在构造函数体内使用赋值运算符更高效。

    class Box {
    public:
        Box(int width, int length, int height) 
            : m_width(width), m_length(length), m_height(height) // member init list
        {}
        int Volume() {return m_width * m_length * m_height; }
    private:
        int m_width;
        int m_length;
        int m_height;
    
    };
    
    

    创建 Box 对象:

    Box b(42, 21, 12);
    cout << "The volume is " << b.Volume();
    

    显式构造函数

     类具有一个类似于下面这样的构造函数:

    Box(int size): m_width(size), m_length(size), m_height(size){}
    

    可以初始化 Box,如下所示:

    Box b = 42;
    

    或将一个 int 传递给采用 Box 的函数:

    class ShippingOrder
    {
    public:
        ShippingOrder(Box b, double postage) : m_box(b), m_postage(postage){}
    
    private:
        Box m_box;
        double m_postage;
    }
    //elsewhere...
        ShippingOrder so(42, 10.8);
    
    
    

     关键字(和用户定义的运算符)以防止出现这种隐式类型转换:

    explicit Box(int size): m_width(size), m_length(size), m_height(size){}
    

    默认构造函数

    默认构造函数没有参数;它们遵循略有不同的规则:

    特殊成员函数;如果没有在类中声明构造函数,则编译器会提供默认构造函数:

    class Box {
        Box(int width, int length, int height) 
            : m_width(width), m_length(length), m_height(height){}
    };
    
    int main(){
    
        Box box1{}; // call compiler-generated default ctor
        Box box2;   // call compiler-generated default ctor
    }
    

    当你调用默认构造函数并尝试使用括号时,系统将发出警告:

    class myclass{};
    int main(){
    myclass mc();     // warning C4930: prototyped function not called (was a variable definition intended?)
    }
    

    如果声明了任何非默认构造函数,编译器不会提供默认构造函数:

    class Box {
        Box(int width, int length, int height) 
            : m_width(width), m_length(length), m_height(height){}
    };
    private:
        int m_width;
        int m_length;
        int m_height;
    
    };
    
    int main(){
    
        Box box1(1, 2, 3);
        Box box2{ 2, 3, 4 };
        Box box4;     // compiler error C2512: no appropriate default constructor available
    }
    
    

    例如,在前面提到的代码块中,框的数组无法进行如下声明:

    Box boxes[3];    // compiler error C2512: no appropriate default constructor available
    
    

    但是,你可以使用初始值设定项列表将框的数组初始化:

    Box boxes[3]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    

    复制和移动构造函数

    显式默认构造函数和已删除构造函数

    派生类中的构造函数

    调用基类构造函数进行派生,例如,如果 ClassA 派生自 ClassB,ClassB 派生自 ClassC,那么首先调用 ClassC 构造函数,然后调用 ClassB 构造函数,最后调用 ClassA 构造函数。

    如果基类没有默认构造函数,则必须在派生类构造函数中提供基类构造函数参数:

    class Box {
    public:
        Box(int width, int length, int height){
           m_width = width;
           m_length = length;
           m_height = height;
        }
    
    private:
        int m_width;
        int m_length;
        int m_height;
    };
    
    class StorageBox : public Box {
    public:
        StorageBox(int width, int length, int height, const string label&) : Box(width, length, height){
            m_label = label;
        }
    private:
        string m_label;
    };
    
    int main(){
    
        const string aLabel = "aLabel";
        StorageBox sb(1, 2, 3, aLabel);
    } 
    

    具有多重继承的类的构造函数

    如果类从多个基类派生,那么将按照派生类声明中列出的顺序调用基类构造函数:

    #include <iostream>
    using namespace std;
    
    class BaseClass1 {
    public:
        BaseClass1() {
            cout << "BaseClass1 constructor." << endl;
        }
    };
    class BaseClass2 {
    public:
        BaseClass2() {
            cout << "BaseClass2 constructor." << endl;
        }
    };
    class BaseClass3{
    public:
        BaseClass3() {
            cout << "BaseClass3 constructor." << endl;
        }
    };
    class DerivedClass : public BaseClass1, public BaseClass2, public BaseClass3  {
    public:
        DerivedClass() {
            cout << "DerivedClass constructor." << endl;
        }
    };
    
    int main() {
        DerivedClass dc;
    }
    
    

    你应看到以下输出:

    BaseClass1 constructor.
    BaseClass2 constructor.
    BaseClass3 constructor.
    DerivedClass constructor.
    

    构造函数中的虚函数

     实现:

    #include <iostream>
    using namespace std;
    
    class BaseClass{
    public:
        BaseClass(){
            print_it();
        }
        virtual void print_it() {
            cout << "BaseClass print_it" << endl;
        }
    };
    
    class DerivedClass : public BaseClass {
    public:
        DerivedClass() {
            print_it();
        }
        virtual void print_it(){
            cout << "Derived Class print_it" << endl;
        }
    };
    
    int main() {
    
        DerivedClass dc;
    }
    

    这是输出:

    BaseClass print_it
    Derived Class print_it
    

    构造函数和复合类

     构造函数中)初始化:

    class Label {
    public:
        Label(const string& name, const string& address) { m_name = name; m_address = address; }
        string m_name;
        string m_address;
    };
    
    class StorageBox : public Box {
    public:
        StorageBox(int width, int length, int height, Label label) 
            : Box(width, length, height), m_label(label){}
    private:
        Label m_label;
    };
    
    int main(){
    // passing a named Label
        Label label1{ "some_name", "some_address" };
        StorageBox sb1(1, 2, 3, label1);
    
        // passing a temporary label
        StorageBox sb2(3, 4, 5, Label{ "another name", "another address" });
    
        // passing a temporary label as an initializer list
        StorageBox sb3(1, 2, 3, {"myname", "myaddress"});
    }
    

    委托构造函数

    在下面的示例中,派生类具有三个构造函数,第二个构造函数委托第一个,第三个构造函数委托第二个:

    #include <iostream>
    using namespace std;
    
    class ConstructorDestructor {
    public:
        ConstructorDestructor() {
            cout << "ConstructorDestructor default constructor." << endl;
        }
        ConstructorDestructor(int int1) {
            cout << "ConstructorDestructor constructor with 1 int." << endl;
        }
        ConstructorDestructor(int int1, int int2) : ConstructorDestructor(int1) {
            cout << "ConstructorDestructor constructor with 2 ints." << endl;
    
            throw exception();
        }
        ConstructorDestructor(int int1, int int2, int int3) : ConstructorDestructor(int1, int2) {
            cout << "ConstructorDestructor constructor with 3 ints." << endl;
        }
        ~ConstructorDestructor() {
            cout << "ConstructorDestructor destructor." << endl;
        }
    };
    
    int main() {
        ConstructorDestructor dc(1, 2, 3);
    }
    
    

    这是输出:

    ConstructorDestructor constructor with 1 int.
    ConstructorDestructor constructor with 2 ints.
    ConstructorDestructor constructor with 3 ints.
    

    DerivedContainer(int int1, int int2)失败,并调用析构函数。

    class ConstructorDestructor {
    public:
        ConstructorDestructor() {
            cout << "ConstructorDestructor default constructor." << endl;
        }
        ConstructorDestructor(int int1) {
            cout << "ConstructorDestructor constructor with 1 int." << endl;
        }
        ConstructorDestructor(int int1, int int2) : ConstructorDestructor(int1) {
            cout << "ConstructorDestructor constructor with 2 ints." << endl;
            throw exception();
        }
        ConstructorDestructor(int int1, int int2, int int3) : ConstructorDestructor(int1, int2) {
            cout << "ConstructorDestructor constructor with 3 ints." << endl;
        }
    
        ~ConstructorDestructor() {
            cout << "ConstructorDestructor destructor." << endl;
        }
    };
    
    int main() {
    
        try {
            ConstructorDestructor cd{ 1, 2, 3 };
        }
        catch (const exception& ex){
        }
    }
    
    

    输出:

    ConstructorDestructor constructor with 1 int.
    ConstructorDestructor constructor with 2 ints.
    ConstructorDestructor destructor.
    

    继承构造函数 (C++11)

    派生类可以使用 using 声明从直接基类继承构造函数,如下面的示例所示:

    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:    
        Base() { cout << "Base()" << endl; }
        Base(const Base& other) { cout << "Base(Base&)" << endl; }
        explicit Base(int i) : num(i) { cout << "Base(int)" << endl; }
        explicit Base(char c) : letter(c) { cout << "Base(char)" << endl; }
    
    private:
        int num;
        char letter;
    };
    
    class Derived : Base
    {
    public:
        // Inherit all constructors from Base
        using Base::Base;
    
    private:
        // Can't initialize newMember from Base constructors.
        int newMember{ 0 };
    };
    
    
    int main(int argc, char argv[])
    {
        cout << "Derived d1(5) calls: "; 
        Derived d1(5);
        cout << "Derived d1('c') calls: ";
        Derived d2('c');
        cout << "Derived d3 = d2 calls: " ;
        Derived d3 = d2;
        cout << "Derived d4 calls: ";
        Derived d4; 
    
        // Keep console open in debug mode:
        cout << endl << "Press Enter to exit.";
        char in[1];
        cin.getline(in, 1);
        return 0;
    }
    
    /* Output:
    Derived d1(5) calls: Base(int)
    Derived d1('c') calls: Base(char)
    Derived d3 = d2 calls: Base(Base&)
    Derived d4 calls: Base()
    
    Press Enter to exit.
    
    

    一般而言,当派生类未声明新数据成员或构造函数时,最好使用继承构造函数。

    如果类型指定基类,则类模板可以从类型参数继承所有构造函数:

    template< typename T >
    class Derived : T {
        using T::T;   // declare the constructors from T
        // ...
    };
    
    

    如果基类的构造函数具有相同签名,则派生类无法从多个基类继承。

    声明构造函数的规则

    。)

     可能为空。

    C++ 定义两种特殊的构造函数(默认构造函数和复制构造函数),如下表所述。

    默认构造函数和复制构造函数

    构造种类

    参数

    用途

    默认构造函数

    可以在没有参数的情况下调用

    构造类类型的默认对象

    复制构造函数

    可以接受对相同类类型的引用的单一参数

    复制类类型的对象

    可以提供多个参数,前提是所有后续参数都有默认值。

    如果使用属于对象但不属于引用的第一个参数指定复制构造函数,则将生成错误。

    仅当基类和成员构造函数存在、可访问并且无歧义时才会调用它们。

    如果基类或成员构造函数存在,则将调用它们;否则将执行按位复制。

     类型的单个参数。

     关键字)与构造函数一起使用将导致编译器错误。

     关键字时,编译器将使用 thiscall 调用约定。

    中所述)。

    显式调用构造函数

     对象,请编写以下代码:

    DrawLine( Point( 13, 22 ), Point( 87, 91 ) );
    

    ,并在表达式(函数调用)的末尾将其销毁。

    在其中显式调用构造函数的另一个上下文正在进行初始化:

    Point pt = Point( 7, 11 );
    

     的对象。

    中更详细地讨论了这一点。

    但是,在构造或析构期间,成员函数调用抽象基类的虚拟成员函数可能是不安全的。

    以下示例演示从构造函数的内部调用虚函数时发生的情况:

    // specl_calling_virtual_functions.cpp
    // compile with: /EHsc
    #include <iostream>
    
    using namespace std;
    class Base
    {
    public:
        Base();             // Default constructor.
        virtual void f();   // Virtual member function.
    };
    
    Base::Base()
    {
        cout << "Constructing Base sub-object
    ";
        f();                // Call virtual member function
    }                       //  from inside constructor.
    
    void Base::f()
    {
        cout << "Called Base::f()
    ";
    }
    
    class Derived : public Base
    {
    public:
        Derived();          // Default constructor.
        void f();           // Implementation of virtual
    };                      //  function f for this class.
    
    Derived::Derived()
    {
        cout << "Constructing Derived object
    ";
    }
    
    void Derived::f()
    {
        cout << "Called Derived::f()
    ";
    }
    
    int main()
    {
        Derived d;
    }
    

     将产生以下事件序列:

    1. ) 的构造函数。

    2. ) 的构造函数。

    请参阅

    特殊成员函数 (C++)

    相关推荐