• 如果类的内部没有专门创建实例的代码,则是无法创建任何实例的
  • 如果父类构造函数设置成了私有的,则子类无法编译,因为在初始化子类时会先执行父类的构造
class A{
public:
private:
    A(int ){
        
    }
};
class B:public A{
public:
    int x;
};
int main( )
{
    B b;
    b.x = 0;
    return 0;
}

输出:

Start
prog.cc:19:7: error: call to implicitly-deleted default constructor of 'B'
    B b;
      ^
prog.cc:13:9: note: default constructor of 'B' is implicitly deleted because base class 'A' has an inaccessible default constructor
class B:public A{
        ^
1 error generated.
1
Finish