开发者

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 thi开发者_如何学运维s class abstract or I have to create dummy fnc and declare it as a pure virtual? Thank you.


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).


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;
}
};


Add pure virtual destructor.


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.


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

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


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...


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).


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


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜