简体   繁体   English

构造函数如何选择C ++中的基类构造函数

[英]How does a constructor choose a base class constructor in C++

如果您在派生类的构造函数内部,并且没有对基类构造函数进行显式调用,则编译器如何知道要使用哪个基本构造函数?

If a base class is not mentioned in the constructor initializer list , it will be default initialized . 如果构造函数初始化器列表中未提及基类,则将默认对其进行初始化 Since the base class will definitely be of class type, that means that the default constructor will be called. 由于基类肯定是类类型的,这意味着将调用默认构造函数

Two of those references also have examples of derived classes which implicitly call the base class default constructor. 这些引用中的两个还具有派生类的示例,这些派生类隐式调用基类的默认构造函数。 For instance: 例如:

struct Class : public Base
{
    unsigned char x;
    unsigned char y;

    Class ( int x )
      : Base ( 123 ), // initialize base class
        x ( x ),      // x (member) is initialized with x (parameter)
        y { 0 }       // y initialized to 0
    {}                // empty compound statement

    Class ( double a )
      : y ( a+1 ),
        x ( y ) // x will be initialized before y, its value here is indeterminate
    {} // base class constructor does not appear in the list, it is
       // default-initialized (not the same as if Base() were used, which is value-init)

   ...
};

It uses the default constructor, as mandated by the standard in N4140 Initializing bases and members , §12.6.2 [class.base.init]/8 (emphasis mine): 它使用N4140 初始化基础和成员中的标准第§12.6.2[class.base.init] / 8(强调我的)中规定的默认构造函数:

In a non-delegating constructor, if a given potentially constructed subobject is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer ), then 在非委托的构造函数中,如果给定的可能构造的子对象未由mem-initializer-id指定(包括由于构造函数没有ctor-initializer而没有mem-initializer-list的情况),则

  • if the entity is a non-static data member that has a brace-or-equal-initializer and either 如果实体是具有大括号或相等初始化程序的非静态数据成员,并且

    • the constructor's class is a union, and no other variant member of that union is designated by a mem-initializer-id or 构造函数的类是一个联合,并且该联合的其他任何变体成员都不由mem-initializer-id
    • the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id , 构造函数的类不是联合体,并且,如果实体是匿名联合体的成员,则该联合体的其他任何成员都不会由mem-initializer-id指定

    the entity is initialized as specified in 8.5; 该实体已按照8.5中的规定进行了初始化;

  • otherwise, if the entity is an anonymous union or a variant member, no initialization is performed; 否则,如果实体是匿名联合或变量成员,则不执行初始化;

  • otherwise, the entity is default-initialized . 否则,实体是默认初始化的

Note that base classes are potentially constructed subobjects per Special member functions , §12 [special]/5: 请注意,根据特殊成员函数 §12[special] / 5,基类是潜在构造的子对象:

For a class, its non-static data members, its non-virtual direct base classes, and, if the class is not abstract, its virtual base classes are called its potentially constructed subobjects. 对于一个类,其非静态数据成员,其非虚拟直接基类,以及(如果该类不是抽象的)其虚拟基类称为其潜在构造的子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C ++中,将基类计数为复制构造函数的构造函数是什么? - In C++, does a constructor that takes the base class count as a copy constructor? 如何在子类c ++的构造函数中设置基类的属性 - How to set an attribute of a base class in a constructor of a sub class c++ C++如何使用派生类构造函数销毁基类中的对象 - C++ how to destroy an object in the base class with a derived class constructor C++派生类构造函数调用基类构造函数错误 - C++ derived class constructor call base class constructor errors 在C ++中派生类构造函数之后调用基类构造函数 - Call base class constructor after the derived class constructor in C++ c++ 中派生的 class 构造函数中的动态基 class 构造函数调用 - dynamic base class constructor call in derived class constructor in c++ C ++:如何根据条件选择构造函数? - C++: how to choose the constructor depending on the condition? 如何将方法结果作为参数传递给C ++中的基类构造函数? - How to pass method result as parameter to base class constructor in C++? 如何在C++中调用Base class的参数化构造函数? - How to call parameterized Constructor of Base class in C++? 如何将名称传递给基类的构造函数 - c++ How to pass a name to the constructor of base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM