Preventing an instance of a base class that is not polymorphic C ++

I have a base class that is not polymorphic, but I want to prevent its creation. Should I give this base class a clean virtual destructor to prevent it from being created? But is it a bad or bad practice to provide a non-polymorphic virtual destructor base class?

+7
c ++ instantiation
source share
3 answers

To prevent the base class from being instantiated, create all protected constructors.

+14
source share

keep ctor / dtor in a protected area.

+1
source share

C ++ base classes are recommended for virtual destructor classes. C ++ is a really old programming language, and in the absence of a virtual destructor, an object of a derived class may be partially or incorrectly destroyed.

Of course, a pure virtual destructor will prevent the creation of instances of this class, but I think that in order to make it clear that you are not expecting to create this class, you can also create protected constructors, as @Niels pointed out in his answer.

Hope this helps.

-6
source share

All Articles