I'm using a C++ base class and subclasses (let's call them A and B for the sake of clarity) in my embedded system.
It's time- and space-critical, so I really need it to be kind of minimal.
The compiler complains about lack of a virtual destructor, which I understand, because that can get you into trouble if you allocate a B*
and later delete the pointer as an instance of A*
.
But I'm never going to allocate any instances of this class. Is there a way I can overload operator new()
such that it compiles if there's no dynamic allocation of either class, but causes a compiler error if an end user tries to allocate new instances of A or B?
I'm looking for a similar approach to the common technique of "poisoning" automatic compiler copy constructors via private constructors. (e.g. http://channel9.msdn.com/Forums/TechOff/252214-Private-copy-constructor-and-private-operator-C)
You can poison
operator new
in just the same way as you can a copy constructor. Just be sure not to poison placement new. A virtual destructor would still be a fine recommendation.Just make operator new private
The elipses are for the other overrides of
operator new
and the rest of the class.