简体   繁体   English

将构造函数库和成员初始化器列表与继承一起使用

[英]Using constructor base and member initializer list with inheritance

I have a question on how to use initializer list for constructors of a derived class that are inheriting from constructors of a base class. 我有一个问题,如何对从基类的构造函数继承的派生类的构造函数使用初始化程序列表。

This is the code that works: 这是有效的代码:

class base {

public:
  base():x(0) {}
  base(int a):x(a) {}

private:
  int x;
};

class derived : public base {

public:
  derived():base():y(0) { y=0; } 
  derived(int a, int b):base(a) { y=b; }

private:
  int y;
};

However, I want to use the member initializer list to initialize the variables directly, and this leads to an error: 但是,我想使用成员初始化程序列表直接初始化变量,这会导致错误:

class base {

public:
  base():x(0) {}
  base(int a):x(a) {}

private:
  int x;
};

class derived : public base {
public:
  //derived():base():y(0) {}  //wrong
  //derived(int a, int b):base(a):y(b) {}  //wrong
  derived():base(), y(0) {}  // corrected
  derived(int a, int b): base(a), y(b) {}  //corrected

private:
  int y;
};

What's the right syntax for constructors that inherits from another constructor to use initializer list? 从另一个构造函数继承来使用初始化程序列表的构造函数的正确语法是什么?

Thanks :) 谢谢 :)

As noted by Dieter, you can easily have many initializers in a constructor, they simply must be separated with comma ( , ) instead of column ( : ). 正如迪特尔指出的,你可以很容易地有许多初始化在构造函数中,他们只是必须用逗号( , )而不是列( : )。

You class derived should then be : 您派生的类应为:

class derived : public base {
public:
  derived():base(),y(0) {} 
  derived(int a, int b):base(a),y(b) {} 

private:
  int y;
};
derivedClass::derivedClass(argumentsSetOne, argumentsSetTwo, .....) : baseClassOne(argumentsSetOne) , baseClassTwo(argumentsSetTwo) { }

order doesn't really matter...by that i mean, you can specify argumentsSetTwo before argumentsSetOne in the Derived Class's Constructor's arguments field. 为了不被真正重要......我的意思是,你可以指定argumentsSetTwoargumentsSetOne在派生类的构造函数中的参数字段。 But again it should be same as specified in the prototype.... 但同样,它应该与原型中指定的相同。

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

相关问题 const引用必须在构造函数base / member初始化列表中初始化 - const reference must be initialized in constructor base/member initializer list 初始化列表:基类的构造函数和成员函数 - initializer list: a constructor from the base class and a member function 对对象的引用必须在构造函数库/成员初始化列表中初始化 - Reference to object must be initialized in constructor base/member initializer list 在构造函数成员初始值设定项列表中使用 std::move() 与分配 - Using std::move() vs. assign in constructor member initializer list 派生类:在初始化列表中使用基类成员 - Derived class: using Base class member in initializer list 为什么不能在派生类的构造函数初始化列表中初始化基类的数据成员? - Why can't Initialize the data member of base class in the constructor initializer list of derived class? 使用具有继承的结构的初始化列表 - Using initializer list for a struct with inheritance 具有成员初始化程序列表的C ++结构构造函数 - C++ struct constructor with member initializer list 构造函数初始化列表中的非成员初始化 - Non-member initialization in constructor initializer list 成员初始值设定项列表中自定义对象数组的构造函数 - Constructor for array of custom objects in member initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM