【发布时间】:2013-05-03 01:34:07
【问题描述】:
struct B {
B () {}
B(int i) {}
};
struct D : B {
using B::B; // <--- new C++11 feature
};
D d1; // ok
D d2(3); // ok
现在,如果我在struct D 的主体内添加一个新的构造函数,例如:
struct D : B {
using B::B;
D(const char* pc) {} // <--- added
};
然后D d1; 开始给出编译器错误(ideone 尚未升级,我使用的是 g++ 4.8.0)?但是D d2(3); 仍然有效。
为什么在struct D中添加新构造函数时默认构造函数会打折?
【问题讨论】:
-
看起来标准规定了这种行为:“对于候选继承构造函数集中的每个非模板构造函数除了没有参数的构造函数或具有单个的复制/移动构造函数参数,构造函数被隐式声明 [...]" [class.inhctor]/3 为什么?不知道。。
-
请注意,您链接到的演示根本不支持继承的构造函数。您可以尝试here,但目前似乎已关闭。
-
@DyP,在代码 sn-p 中你没有添加新的
D(const char*)构造函数 -
@iammilind m(你是对的..
-
@iammilind 无法发布指向“正确”版本的实时示例的链接,链接太长,无法发表评论...... :(
标签: c++ c++11 constructor using default-constructor