【发布时间】:2013-12-24 12:14:49
【问题描述】:
在这段代码中,为什么没有继承A的无参数构造函数?是否有特殊规则禁止继承无参数的构造函数?
struct A {
A(void *) {}
A() {}
};
class B : public A {
public:
using A::A;
B(int x) {}
};
void f() {
B b(1);
B b2(nullptr);
B b3; // error
}
clang++ -std=c++11 给出这个错误,而 g++ -std=c++11 给出一个基本相似的错误消息:
td.cpp:15:7: error: no matching constructor for initialization of 'B'
B b3; // error
^
td.cpp:9:5: note: candidate constructor not viable: requires single argument 'x', but no arguments
were provided
B(int x) {}
^
td.cpp:8:14: note: candidate constructor (inherited) not viable: requires 1 argument, but 0 were
provided
using A::A;
^
td.cpp:2:5: note: inherited from here
A(void *) {}
【问题讨论】:
标签: c++ inheritance c++11 constructor