C ++:任何方式来防止抽象基类的任何实例化? [英] C++: any way to prevent any instantiation of an abstract base class?
问题描述
除了有一个纯虚函数,有没有办法阻止抽象基类的实例化?
我可以这样做:
class BaseFoo
{
virtual void blah()= 0;
};
class Foo:public BaseFoo
{
virtual void blah(){}
};
但我想避免vtable。 (按照我有关虚拟析构函数的其他问题)
Microsoft ATL有ATL_NO_VTABLE来实现这一点(至少我认为这是它的作用...)
这当然会将corectness的负担转移到派生类,但至少基类是受保护的。
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 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.
这篇关于C ++:任何方式来防止抽象基类的任何实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!