Question

I thought of using protected constructor, but it couldn't completely solve the purpose since the class inheriting from it would be able to instantiate the base class.

As for private constructor, the derived classes too would not be instantiated.

So, any suitable technique would be appreciated.

Was it helpful?

Solution

It is unclear what you are really asking for. So let me try to clear some points:

Pure virtual functions can have definitions

If your concern is that you want to provide definitions for all of the virtual functions in your base you can provide the definitions for the pure virtual functions, and they will be available for static dispatch.

Protected grants access to your base subobject, not to every instance of base

There is a common misconception that protected allows a particular derived type accessing any instance of base. That is not true. The keyword protected grants access to the base subobject within the derived type.

class base {
protected: base() {}
};
class derived : public base {
   derived() : base() {         // fine our subobject
      base b;                   // error, `b` is not your subobject
   }
};

OTHER TIPS

The definition of an abstract class is one that has at least one pure virtual function (virtual function-signature = 0; You can't create an abstract class without them.

Can an abstract class be implemented without pure virtual functions in C++?

If you choose the point of view from Static Polymorphism, you can do that!

An abstract base class would be simply missing a default method implementation for an interface method from the deriving class.

Additionally you can use protected constructors for those CRTP base class templates, to require inheritance for instantiation.

UPDATE:
I found a nice slide show, that explains static vs dynamic polymorphism comprehensively. Each technique has it's pros and cons and certain fields of usage, additionally you can mix both techniques (wisely of course).

To elaborate a bit, I'll give a sample:

template<class Derived>
class AbstractBase
{
public:
    // Equivalent for a pure virtual function
    void foo()
    {
        // static_cast<> enforces an 'Is a' relation from Derived to AbstractBase
        static_cast<Derived*>(this)->fooImpl();
    }

    // Equivalent for simple virtual function (overidable from Derived)
    void bar()
    {
        static_cast<Derived*>(this)->barImpl();
    }

    // Default implementation for any call to bar()
    void barImpl()
    {
    }

protected:
    AbstractBase() {}
};

// Compilation will fail, since ConcreteClass1 doesn't provide 
// a declaration for fooImpl()
class ConcreteClass1
: public AbstractBase<ConcreteClass1>
{
}


// Compiles fine
class ConcreteClass2
: public AbstractBase<ConcreteClass2>
{
public:

    void fooImpl()
    {
        // Concrete implementation ...
    }
}

The following sample shows that the pattern introduced above enforces an 'Is a' relationship between abstract class and inheriting class (the template parameter)

class ConcreteClass3
{
public:
    void fooImpl()
    {
        // Concrete implementation ...
    }
}   

// Instantiation will fail, because 
// * the constructor is protected
// * at least the static cast will fail
AbstractBase<ConcreteClass3> instance; 

I read it in my book An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top