[英]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
地方传递Ant
和Spider
对象(即,不使用更多模板)。
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.