kov*_*rex 2 c++ constructor default c++11
从C++ 11开始,我们有这个很棒的功能,它允许我们避免为所有小类创建显式构造函数,例如:
class A
{
public:
A() = default;
A(int x, int y) : x(x), y(y) {} // bloat
int x = 0, y = 0;
};
..
A a(1,2);
Run Code Online (Sandbox Code Playgroud)
所以我们现在可以这样写:
class A
{
public:
int x = 0, y = 0;
};
..
A a{1,2}; // using the sequence constructor created by the compiler, great
Run Code Online (Sandbox Code Playgroud)
问题出现了,当我还有其他我想要使用的构造函数时,例如:
class A
{
public:
A() = default;
A(Deserialiser& input) : a(input.load<int>()), b(input.load<int>()) {}
int x = 0, y = 0;
};
...
A a{1, 2}; // It doesn't work now, as there is another constructor explicitly specified
Run Code Online (Sandbox Code Playgroud)
问题是,如何强制编译器创建默认序列构造函数?
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |