I have a base class that is not polymorphic, but I want to prevent it from being instantiated. Should I give this base class a pure virtual destructor to prevent it from being instantiated? But is it wrong or bad practice to give a non-polymorphic base class a virtual destructor?
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.
Private constructor: It will prevent to instantiate the Singleton class from outside the class. Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.
To prevent a base class from being instantiated make all constructors protected
.
keep the ctor/dtor in protected scope.
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