C++: any way to prevent any instantiation of an ab

2019-04-04 05:57发布

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...)

3条回答
冷血范
2楼-- · 2019-04-04 06:32

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-04-04 06:37

If you make a protected constructor as advised here, then when your derived class is constructed you'll get an error akin to, "cannot access private member declared in class", with some other information specific to your classes.

If you have pure virtual methods in your base class, then the problem isn't that those are instantiated (and it's certainly not that non-abstract methods are instantiated), but the problem occurs at destruction time when the compiler can not infer what your derived class owns. That, or you have instantiated stuff without an owner! (ruh roh rhaggy)

Declare a pure virtual destructor for your base class and then implement it externally. Also, never, ever make a constructor private (edit: unless it is guaranteed to only be used internally, such as automatic construction of the next node in a linked list). The closest you'll ever want (edit: otherwise) is an explicit constructor (but that's another topic).

edits: I may have found the answer, but I still can't type today.

// example.h

class A
{

    A ( ) { }
    virtual ~A ( ) = 0;
};

class B : public A
{
    B ( ) { }
    ~B ( ) { }
};

// example.cpp
#include "example.h"

A::~A ( ) { }
查看更多
Summer. ? 凉城
4楼-- · 2019-04-04 06:52

You could make a protected constructor

查看更多
登录 后发表回答