jeudi 16 avril 2015

Ensure abstract bass class is a shared_ptr

I have an abstract base class:



struct Base : std::enable_shared_from_this<Base>
{
virtual ~Base() = default;
virtual void foo() = 0;

void bar() {
baz(shared_from_this());
}
};


The only valid use case for Base is to live in a shared_ptr - bar is an important method. How can I ensure that the following is impossible:



struct BadDerived : Base {
void foo() override { ... }
};

BadDerived bd;
bd.bar();

Aucun commentaire:

Enregistrer un commentaire