pure virtual function and abstract class

試著忘記壹切 提交于 2019-12-20 04:26:15

问题


I have the following classes, Base and Derived and when I compile the compiler complains that it cannot create an instance of DLog because it is abstract.

Can someone tell me how I can fix this error?

I'm guessing it's because not both pure virtual functions are not implemented in the Derived.

class Logger
{
public:

    virtual void log(int debugLevel, char* fmt, ...) = 0;
    virtual void log(string logText, int debugLevel, string threadName = "") = 0;

    static Logger* defaultLogger() {return m_defaultLogger;}
    static void setDefaultLogger(Logger& logger) {m_defaultLogger = &logger;}

protected:

    static Logger* m_defaultLogger;
};

class DLog : public Logger
{
public:
    DLog();
    ~DLog();

    static DLog *Instance();
    static void Destroy();

    void SetLogFilename(std::string filename);
    void SetOutputDebug(bool enable);
    std::string getKeyTypeName(long lKeyType);
    std::string getScopeTypeName(long lScopeType);
    std::string getMethodName(long lMethod);

    virtual void log(string logText, int debugLevel)
    {
        Log(const_cast<char*>(logText.c_str()));
    }

    void Log(char* fmt, ...);

private:

    static DLog *m_instance;

    std::string m_filename;
    bool m_bOutputDebug;
};

// DLog instantion as a singleton

DLog *DLog::Instance()
{
    if (!m_instance)
        m_instance = new DLog();
    return m_instance;
}

回答1:


virtual void log(string logText, int debugLevel, string threadName = "") = 0;

has not been implemented in class DLog. You have to implement it because it's pure virtual in the base class.

You probably meant this in your first overload of log in DLog:

virtual void log(string logText, int debugLevel, string /*threadname*/)
{
    Log(const_cast<char*>(logText.c_str()));
}

EDIT: You also have not implemented the overload of

virtual void log(int debugLevel, char* fmt, ...) = 0;

Note here though that using the const_cast is a very bad idea and is undefined behavior. You can get well defined behavior by doing something like this instead:

virtual void log(string logText, int debugLevel, string /*threadname*/)
{
    logText.push_back('\0'); // Add null terminator
    Log(&logText[0]); // Send non-const string to function
    logText.pop_back(); // Remove null terminator
}

Better yet though, just make "Log" const-correct in the first place.




回答2:


By deriving your DLog class from Logger you are assuring that you will provide the implementation for all (assuming you don't want DLog as an abstract class) pure virtual methods declared in the base class. Here you have not provided implementation to pure virtual functions hence class DLog becomes an abstract class. In C++ you can not create an instance of an abstract class hence you get the compiler error. BTW, your base class is missing the virtual destructor.



来源:https://stackoverflow.com/questions/3185954/pure-virtual-function-and-abstract-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!