C ++:任何方式来防止抽象基类的任何实例化? [英] C++: any way to prevent any instantiation of an abstract base class?

查看:235
本文介绍了C ++:任何方式来防止抽象基类的任何实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了有一个纯虚函数,有没有办法阻止抽象基类的实例化?



我可以这样做:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆