Abstract class in c++

Let's say I've got class:

class Bad_Date
{
private:
const char* _my_msg;
public:
const char* msg() const
{
return _my_msg;
}
};

And I would like to not be able to create any object of this class but I don't really want to put anything else there and make it pure virtual fnc. Is there any other way to make this class abstract or I have to create dummy fnc and declare it as a pure virtual? Thank you.

Upvotes: 1

Views: 1143

Answers (9)

KeatsPeeks
KeatsPeeks

Reputation: 19327

If you need a base class, you may need a virtual destructor. Make it pure virtual and you've got your abstract class.

If you don't need a virtual destructor (ie the class is not used polymorphically), you can make the constructor protected (not private).

Upvotes: 10

Rahul Goyal
Rahul Goyal

Reputation: 1

You could also make the msg(), pure virtual and also providing its implementation at the same time.

class Bad_Date
{
private:
const char* _my_msg;
public:
virtual const char* msg() const = 0;
};

const char* Bad_Date::msg() const {
    return _my_msg;
}

Upvotes: 0

rmn
rmn

Reputation: 2426

You should read this for avoiding common pitfalls with pure virtual destructors.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340178

Make the destructor pure virtual (abstract) - but remember to also give it an implementation, otherwise you'll never be able to destroy objects derived from Bad_Date:

class Bad_Date
{
    // ...

public:  // or maybe protected

    virtual ~Bad_Date() = 0;
};


Bad_Date::~Bad_Date()
{
}

Many people are confused by the combination of pure virtual functions that are also defined, thinking that it makes no sense. But it's not only legal, it's required in some cases (like a pure virtual dtor).

Upvotes: 0

Mykola Golubyev
Mykola Golubyev

Reputation: 59804

Add pure virtual destructor.

Upvotes: 7

Logan Capaldo
Logan Capaldo

Reputation: 40336

You can make the destructor pure virtual, and have that be your "dummy function". i.e.

 class Bad_Date
 {
   private:
   const char* _my_msg;
   public:
   const char* msg() const { return _my_msg; }
   virtual ~Bad_Date() = 0;
 };

Making the destructor virtual is a good idea anyway for any class you intend to use polymorphicaly, to ensure that subclass instances get cleaned up appropriately. If you need Bad_Date to do some work in the destructor though, you can't make the destructor pure virtual of course. Making Bad_Date's constructor(s) protected is another viable technique. This will ensure that a Bad_Date can only be constructed by a subclass of Bad_Date. Unfortunately this won't prevent someone from creating a Bad_Date subclass just to act as factory for Bad_Dates.

Beyond that there are compiler specifc extensions for creating abstract base classes, e.g. MSVC has __interface and gcc has used to have signature.

Upvotes: 3

jackrabbit
jackrabbit

Reputation: 5653

My C++ is rusty, but still:

Maybe you can give the class only a private constructor?

IIRC, abstract classes are abstract precisely because they have at least one pure virtual function...

Upvotes: 0

DigitalZebra
DigitalZebra

Reputation: 41473

Make the constructor protected:

class Bad_Date
{
private:
const char* _my_msg;

// Add the 2 lines below:
protected:
    Bad_Date() {}

public:
const char* msg() const
{
return _my_msg;
}
};

Upvotes: 7

int3
int3

Reputation: 13201

You could make the constructor private protected, thus overriding the default constructor:

class Bad_Date
{
    protected:
    Bad_Date() { }
    // rest of the class definition
};

Upvotes: 1

Related Questions