简体   繁体   English

默认基类初始化器

[英]Default base class initializer

Is there a way to specify a default initializer for a base class in the class declaration? 有没有一种方法可以在类声明中为基类指定默认的初始化程序?

Say I have: 说我有:

#include <string>

struct Animal {
    Animal(int legs) : legs{legs} {}
    Animal() = default;
    int legs{4};
};

struct Ant : Animal {
    Ant(int size)       : Animal{6}, size{size} {}
    Ant()               : Animal{6} {}
    Ant(std::string s)  : Animal{6} {}
    int size{0};
};

struct Spider : Animal {
    Spider(int size)       : Animal{8}, size{size} {}
    Spider()               : Animal{8} {}
    Spider(std::string s)  : Animal{8} {}
    int size{0};
};

Is there a way to specify a default base initializer similar to the default member initializer ? 有没有办法指定类似于默认成员初始化程序默认基本初始化 程序

Something like perhaps: 大概是这样的:

#include <string>

struct Animal {
    Animal(int legs) : legs{legs} {}
    Animal() = default;
    int legs{4};
};

struct Ant : Animal{6} {
    Ant(int size)       : size{size} {}
    Ant()                {}
    Ant(std::string s)   {}
    int size{0};
};

struct Spider : Animal{8} {
    Spider(int size)       : size{size} {}
    Spider()                {}
    Spider(std::string s)   {}
    int size{0};
};

I thought about using delegating constructors, but if there are multiple bases and different constructors where the same bases are not always default initialized then delegating constructors wouldn't help to simplify in the same way as a constructor member initializer list would do for members. 我考虑过使用委派构造函数,但是如果存在多个基础和不同的构造函数,而其中的基础并非总是默认初始化,那么委派构造函数将无济于事,就像构造函数成员初始化程序列表对成员的简化一样。

Is there a way to specify a default initializer for a base class in the class declaration? 有没有一种方法可以在类声明中为基类指定默认的初始化程序?

To do that in the class declaration itself would require making the base class have a template parameter, eg: 为此,需要在类声明本身中使基类具有模板参数,例如:

template <size_t NumberOfLegs>
struct Animal {
    Animal() = default;
    int legs{NumberOfLegs};
};

struct Ant : Animal<6> {
    Ant()               = default;
    Ant(int size)       : size{size} {}
    Ant(std::string s)  {}
    int size{0};
};

struct Spider : Animal<8> {
    Spider()               = default;
    Spider(int size)       : size{size} {}
    Spider(std::string s)  {}
    int size{0};
};

But that means Animal<6> and Animal<8> are different class types, and you won't be able to pass around Ant and Spider objects where Animal is expected (without using more templates, that is). 但这意味着Animal<6>Animal<8>是不同的类类型,并且您将无法在需要Animal地方传递AntSpider对象(即,不使用更多模板)。

That being said, assuming you actually want Animal to be a distinct class you can derive from and pass around, I suggest you could give your int constructors default values and omit the = default constructors (and yes, you can use delegating constructors in this example), eg: 话虽这么说,假设您实际上希望Animal是一个可以从中派生并传递的独特类,我建议您可以为int构造函数提供默认值,并省略= default构造函数(是的,您可以在此示例中使用委托构造函数),例如:

struct Animal {
    Animal(int legs = 4) : legs{legs} {}
    int legs;
};

struct Ant : Animal {
    Ant(int size = 0)   : Animal{6}, size(size) {}
    Ant(std::string s)  : Ant(0) {}
    int size;
};

struct Spider : Animal {
    Spider(int size = 0)   : Animal{8}, size{size} {}
    Spider(std::string s)  : Spider(0) {}
    int size;
};

暂无
暂无

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

相关问题 派生 class 构造函数初始值设定项列表中的基本 class 默认构造函数 - Base class default constructor in derived class constructor initializer list 为什么派生类的构造函数在初始化列表中使用基类的默认构造函数? - Why should a derived class's constructor use the base's default constructor in it's initializer list? 在继承的基类的成员上使用指定初始化器 - Using Designated Initializer on member of inherited base class 当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类” - Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name 为什么需要在派生类的初始化列表中调用基类的构造函数? - Why the constructor for a base class needs to be called in the initializer list of the derived class? 防止在无效的派生类初始值设定项上调用基类构造函数 - Prevent Base class constructor being called on invalid Derived class initializer 派生类:在初始化列表中使用基类成员 - Derived class: using Base class member in initializer list 错误:过时的旧式基类初始化程序[-fpermissive] - error: anachronistic old-style base class initializer [-fpermissive] 初始化列表:基类的构造函数和成员函数 - initializer list: a constructor from the base class and a member function 错误:不合时宜的旧式基类初始值设定项 - error: anachronistic old-style base class initializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM