构造函数 (C++)

构造函数是一个特殊的成员函数,可将函数类的实例初始化。 若要调用构造函数,请使用大括号或括号内的类名称和参数。

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 SomeClass{
public:
    static void set_box(const Box& aBox) {}
};
int main(){

    Box box1(1, 2, 3);
    Box box2{ 2, 3, 4 };
    SomeClass::set_box(Box{ 5, 6, 7 });
}

有关详细信息,请参阅声明构造函数的规则。 有关初始化的详细信息,请参阅初始化

构造函数顺序

构造函数按此顺序执行工作:

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

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

  3. 如果类具有或继承了虚函数,则会将对象的虚函数指针初始化。 虚函数指针指向类中的虚函数表,确保虚函数正确地调用绑定代码。

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

下面的示例显示,在派生类的构造函数中,基类和成员构造函数的调用顺序。 首先,调用基构造函数,然后按照基类成员在类声明中出现的顺序对这些成员进行初始化,然后,调用派生构造函数。

#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. 如果是非委托构造函数,所有完全构造的基类对象和成员均将被销毁。 但是,对象本身不是完全构造的,因此析构函数不会运行。

显式构造函数

如果构造函数只有一个参数,或者除了一个函数之外,所有函数都有默认值,那么使用构造函数中的 explicit 关键字可以防止隐式类型转换。 有关详细信息,请参阅构造函数 (C++)

默认构造函数

默认构造函数(即没有参数的构造函数)遵从的原则略有不同。

如果类中没有声明构造函数,则编译器提供默认构造函数:

class Box {
    int m_width;
    int m_length;
    int m_height;
};

int main(){

    Box box1{};
    Box box2;
}

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

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

这是“最棘手的解析”问题的示例。 这种示例表达式既可以解释为函数的声明,也可以解释为对默认构造函数的调用,而且 C++ 分析器更偏向于声明,因此表达式会被视为函数声明。 有关详细信息,请参阅最棘手的解析

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

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;

};

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 } };

复制和移动构造函数

复制构造函数通过对对象的引用进行复制。 如果你没有提供类的附件构造函数,编译器将创建默认附件构造函数,即使已声明了附件操作、移动操作或析构函数,也是如此。 有关详细信息,请参阅声明构造函数的规则

移动构造函数可启用一个对象到另一个对象的已分配内存传输。 有关详细信息,请参阅如何:编写移动构造函数

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

你可以显式默认复制构造函数、默认构造函数、复制赋值运算符和析构函数,但是不支持显式默认移动构造函数和移动赋值运算符。 (Visual Studio 2015 中存在此支持。) 你可以显式删除所有特殊函数。 有关详细信息,请参阅显式默认设置的函数和已删除的函数

派生类中的构造函数

派生类构造函数始终调用基类构造函数,因此,在完成任何额外任务之前,它可以依赖于完全构造的基类。 调用基类构造函数进行派生,例如,如果 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.

构造函数中的虚函数

我们建议你谨慎调用构造函数中的虚函数。 基类构造函数始终在派生类构造函数之前调用,因此基构造函数中调用的函数是基类版本,而非派生类版本。 在下面的示例中,构造 DerivedClass 会导致执行 BaseClassprint_it() 实现早于 DerivedClass 构造函数导致执行 DerivedClassprint_it() 实现:

#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

构造函数和复合类

包含类类型成员的类称为“复合类”。 创建复合类的类类型成员时,调用类自己的构造函数之前,先调用构造函数。 当包含的类没有默认构造函数是,必须使用复合类构造函数中的初始化列表。 在之前的 StorageBox 示例中,如果将 m_label 成员变量的类型更改为新的 Label 类,则必须调用基类构造函数,并且将 m_label 变量(位于 StorageBox 构造函数中)初始化:

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) 成功,但是 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++)