开发者

C++: any way to prevent any instantiation of an abstract base class?

开发者 https://www.devze.com 2023-03-10 22:47 出处:网络
Aside from having a pure virtual function, is there a way to prevent an instantiation of an abstract base class?

Aside from having a pure virtual function, is there a way to prevent an instantiation of an abstract base class?

I can do this:

class BaseFoo
{
    virtual void blah() = 0;
};

class Foo : public BaseFoo
{
    virtual void blah() {}
};

but I'd like to avoid a vtable. (as per my other question about virtual destructors)

Microsoft ATL has ATL_NO_VTABLE to accomplish this (or at开发者_JAVA技巧 least I think that's what it does...)


A really obvious way is to declare a protected constructor, and to declare public constructors in the non-abstract derived classes.

This of course shifts the burden of corectness to the derived classes, but at least the base class is protected.


You could make a protected constructor

0

精彩评论

暂无评论...
验证码 换一张
取 消