【发布时间】:2013-02-08 09:21:36
【问题描述】:
代码:
struct A
{
private:
A() = default; // Version 1.
};
struct B : public A
{};
struct C
{
private:
C() {}; // Version 2.
};
struct D : public C
{};
int main()
{
B b; // Compiles under g++ 4.7.2
D d; // Compilation error under g++ 4.7.2
}
还有两种情况(使用 gcc 4.7.2):
- 如果我编译这段代码(使用版本 1 的 A 的构造函数),没有问题。
- 如果我使用第二个构造函数,gcc 会告诉我
D::D()是私有的。
问题:如果我使用默认构造函数,为什么问题会消失?如果A 具有私有构造函数,则其他类永远不能创建A 的实例,甚至不能创建其派生类,无论其构造函数实现的“默认性”如何。
【问题讨论】:
标签: c++ inheritance c++11 private default-constructor