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 nee
Just make operator new private
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.
int main() {
char data[sizeof(Derived)];
if (condition)
new (data) Derived();
else
new (data) Base();
Base* ptr = reinterpret_cast<Base*>(&data[0]);
ptr->~Base();
}
class A
{
private:
void *operator new(size_t);
...
};
The elipses are for the other overrides of operator new
and the rest of the class.