简体   繁体   中英

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.

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

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

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

您应该阅读此内容,以避免使用纯虚拟析构函数的常见陷阱。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM