Reputation: 24675
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
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
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
Reputation: 2426
You should read this for avoiding common pitfalls with pure virtual destructors.
Upvotes: 0
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
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_Date
s.
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
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
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
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