Search code examples
c++inheritanceconstructordefault-constructor

Forbid using default constructor in derived classes, C++


Is there any way to create base class (such as boost::noncopyable) and inherit from it, which will forbid compiler to generate default constructor for derived classes, if it wasn't made by user (developer)?

Example:

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

class Child : YYY SuperDad {
public:
  Child(int a) {...}
};

And result:

int main () {
  Child a;     // compile error
  Child b[7];  // compile error
  Child c(13); // OK
}

Solution

  • Make the constructor private.

    protected:
        Base() = default;