Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - virtual destructors and linker errors

I got this interface I've written:

#ifndef _I_LOG_H
#define _I_LOG_H

class ILog {
public:
    ILog();
    virtual ~ILog();

    virtual void LogInfo(const char* msg, ...) = 0;
    virtual void LogDebug(const char* msg, ...) = 0;
    virtual void LogWarn(const char* msg, ...) = 0;
    virtual void LogError(const char* msg, ...) = 0;

private: 
    Monkey* monkey;
};

#endif

The methods are pure virtual and therefore must be implemented by deriving classes. If I try to make a class that inherits this interface I get the following linker errors:

Undefined reference to ILog::ILog
Undefined reference to ILog::~ILog

I understand why there is a virtual destructor (to make sure the derived's destructor is called) but I do not understand why I get this linker error.

EDIT: Okay, so I need to define the virtual destructor as well. But can I still perform stuff in the definition of the virtual destructor, or will it simply call my derived classes destructor and skip it? Like, will this trigger:

virtual ~ILog() { delete monkey; }
like image 793
KaiserJohaan Avatar asked Dec 16 '22 05:12

KaiserJohaan


1 Answers

You haven't defined the constructor and destructor, you have only declared them

Try

class ILog {
public:
    //note, I want the compiler-generated default constructor, so I don't write one
    virtual ~ILog(){} //empty body

    virtual void LogInfo(const char* msg, ...) = 0;
    virtual void LogDebug(const char* msg, ...) = 0;
    virtual void LogWarn(const char* msg, ...) = 0;
    virtual void LogError(const char* msg, ...) = 0;
};
  • Constructor: Once you declare a constructor, any constructor, the compiler doesn't generate a default constructor for you. The constructor of the derived class tries to call the interface's constructor and it is not defined, just declared. Either provide a definition, or remove the declaration
  • Destructor: Other considerations left alone (for example, similar considerations as above) your destructor is virtual. Every non-pure virtual function must have a definition (because it is by definition used).

can I still perform stuff in the definition of the virtual destructor, or will it simply call my derived classes destructor and skip it? Like, will this trigger

Yes you can. When the destructor of the derived class is called, it will automatically call the base class's destructor. However there isn't much I can think of that it would make sense to do in a destructor of an interface. But technically you can do anything in the destructor, even if it is virtual

like image 98
Armen Tsirunyan Avatar answered Dec 29 '22 21:12

Armen Tsirunyan