禁止在派生类C++中使用默认构造函数

Tro*_*iar 7 c++ inheritance constructor default-constructor

有没有办法创建基类(如boost :: noncopyable)并从中继承,如果它不是由用户(开发人员)制作的,它将禁止编译器为派生类生成默认构造函数?

例:

class SuperDad {
XXX:
  SuperDad(); // = delete?
};

class Child : YYY SuperDad {
public:
  Child(int a) {...}
};
Run Code Online (Sandbox Code Playgroud)

结果:

int main () {
  Child a;     // compile error
  Child b[7];  // compile error
  Child c(13); // OK
}
Run Code Online (Sandbox Code Playgroud)

Tro*_*iar 6

使构造函数私有.

protected:
    Base() = default;
Run Code Online (Sandbox Code Playgroud)