struct A
{};
struct B : A
{
operator A() const = delete; // not work
};
int main()
{
B* p_derived = new B();
delete p_derived; // ok
// How to make the following two lines illegal?
A* p_base = new B();
delete p_base;
}
A's definition cannot be changed and its destructor is not virtual; so B's object shouldn't be deleted by a pointer to A.
I can't use struct B : protected A {};, because I want B to inherit all public members of A and keep them still public.
In C++'s mixin patterns, this is a common problem:
This is ok unless the mixin class is deleted polymorphically through a pointer to the original class.
My question:
How to prevent an object from being deleted via a pointer to its parent type?