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
}
A derived class cannot have a constructor with default parameters. ____ 16. Default arguments can be used with an overloaded operator.
Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden.
If we inherit a class from another class and create an object of the derived class, it is clear that the default constructor of the derived class will be invoked but before that the default constructor of all of the base classes will be invoke, i.e the order of invocation is that the base class's default constructor ...
Typically, constructors have public accessibility so that code outside the class definition or inheritance hierarchy can create objects of the class. But you can also declare a constructor as protected or private . Constructors may be declared as inline , explicit , friend , or constexpr .
Make the constructor private.
protected:
Base() = default;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With