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

后端 未结 3 604
我在风中等你
我在风中等你 2021-02-19 01:19

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
{
    virtu         


        
相关标签:
3条回答
  • 2021-02-19 01:36

    You could make a protected constructor

    0 讨论(0)
  • 2021-02-19 01: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 ( ) { }
    
    0 讨论(0)
  • 2021-02-19 01:41

    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.

    0 讨论(0)
提交回复
热议问题