简体   繁体   English

如果不为具有不同签名的不同派生构造函数调用基类的构造函数,会发生什么情况?

[英]What happens if you don't call a base class's constructor for different derived constructors with different signatures?

Say I have a base and derived class like this:假设我有一个 base 和 derived class 这样的:

class Base {
public:
    Base() { ... };
    Base(int param) { ... };
};

class Derived : public Base {
public:
    Derived() { ... };
    Derived(int param) { ... };
    Derived(int p1, int p2) { ... };
};

Note that I'm not explicitly calling any of Base 's constructors for Derived 's constructors.请注意,我没有Derived的构造函数显式调用Base的任何构造函数。 That is to say, I didn't write Derived(): Base() {... } or Derived(int param): Base(param) {... } .也就是说,我没有Derived(): Base() {... }Derived(int param): Base(param) {... }

Do any of Base 's constructors get called by default when creating an instance of Derived ?创建Derived的实例时,默认情况下会调用Base的任何构造函数吗?

If so, does Base(int param) get called when using Derived(int param) , or does Base() get called?如果是这样,是在使用Derived(int param)时调用Base(int param) ) 还是调用Base()

Put another way, does C++ always default to using a base class's constructor with the same signature as the derived class's constructor if you don't specify which constructor to use?换句话说,如果您不指定要使用哪个构造函数,C++ 是否总是默认使用与派生类的构造函数具有相同签名的基类构造函数? Or does it just use the base class's default constructor?或者它只是使用基类的默认构造函数?

If the former, what about when using a constructor in the derived class that doesn't have a matching constructor with the same signature in the base class, such as Derived(int p1, int p2) ?如果是前者,那么在派生的 class 中使用构造函数时,如果在基类 class 中没有具有相同签名的匹配构造函数,例如Derived(int p1, int p2)怎么办?

Please note this question does not relate to initialization of member variables of either class. I intentionally did not include any member variables in my pseudo-code.请注意这个问题与 class 的成员变量的初始化无关。我有意没有在我的伪代码中包含任何成员变量。 It specifically has to do with which constructor on the base class gets used if you do not explicitly specify a base constructor in the derived class's constructors.如果您没有在派生类的构造函数中显式指定基构造函数,它特别与使用基 class 上的哪个构造函数有关。

Quoting from cppreference's description of constructors and how inheritance relates to them:引用 cppreference 对构造函数的描述以及 inheritance 与它们的关系:

Before the compound statement that forms the function body of the constructor begins executing, initialization of all direct bases , virtual bases, and non-static data members is finished.在构造函数主体forms function 复合语句开始执行之前,所有直接基类、虚基类和非静态数据成员的初始化已经完成。 Member initializer list is the place where non-default initialization of these objects can be specified.成员初始化器列表是可以指定这些对象的非默认初始化的地方。 For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.对于不能默认初始化的基类和非静态数据成员,例如引用成员和 const 限定类型,必须指定成员初始化器。 No initialization is performed for anonymous unions or variant members that do not have a member initializer.不对没有成员初始值设定项的匿名联合或变体成员执行任何初始化。

(Emphasis added.) (强调已添加。)

If you do not specify an initialization of the base class subobject in your derived class's constructor's member initializer list, the base class subobject is default-initialized.如果您没有在派生类的构造函数的成员初始化列表中指定基类 class 子对象的初始化,则基类 class 子对象将被默认初始化。

暂无
暂无

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

相关问题 如果不从派生构造函数调用基本构造函数,会发生什么? - What happens if you don't call the base constructor from the derived constructor? 将变量传递给构造函数到派生的 class 会给出垃圾值,基类和派生类的构造函数都有不同的参数。 为什么? - Passing variable to constructor to a derived class gives garbage value, both the base and derived class's constructors have different parameters. Why? 为具有不同签名功能的派生类设计基类 - Design base class for derived classes with functions having different signatures C ++:在另一个派生类的对象的基本指针上调用(派生的)成员函数 - C++: call (derived's) member function on base pointer of a different derived class's object 派生类中的基本构造函数调用 - Base Constructor Call in Derived Class 你可以调用基类的构造函数来创建派生对象吗? - Can you call a constructor for a base class to create a derived object? 对派生的 class C++ 使用不同的基本构造函数 - Using a different base constructor for a derived class C++ 派生类构造函数调用基本构造函数 - derived class constructor call base constructor 在基础与派生 class 中覆盖具有不同访问权限的方法有什么问题 - What's the problem(s) with overriding a method with different access in base vs derived class C ++:在不同类的派生对象的基本指针上调用静态成员函数 - C++: call static member function on base pointer of different class's derived object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM