Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ abstract class with some implementation

I want to make an abstract class in c++ with a single, but with some default implementation. so that every class that inherits it will have default behavior but you cant create an instance of the base class. but if i mark foo as pure virtual, I can't add an implementation to it.

class Base
{
public:
    virtual void foo() =0; //Now I can't add foo implementation
};

My solution was to not have it as a pure virtual, and just hide the constructor. I'm wondering if its possible to mark the class as pure, but still have some implementation?

like image 691
Bg1987 Avatar asked Jan 10 '12 07:01

Bg1987


1 Answers

You can add an implementation to a pure virtual function. Classes that derive can use the default implementation by explicitly invoking the base-class-method.

like image 141
Björn Pollex Avatar answered Sep 24 '22 09:09

Björn Pollex