3

I have a C++ class Logger that implements Logger_IF which has one pure virtual function defined:

virtual void log( string message ) = 0;

I would like to extend Logger to my own class with it's own interface. I have called my class SpecialLogger which extends SpecialLogger_IF. SpecialLogger_IF itself extends Logger. I have defined:

virtual void log( string message, string extra ) = 0;

on my SpecialLogger_IF class. As I understand this, I should be able to still call log("message") through SpecialLogger_IF as well as log("message", "extra") however I cannot get this to work.

#include <iostream>

using namespace std;

class Logger_IF
{
    public:

        virtual void log( string message ) = 0;
        virtual ~Logger_IF() {};
};

class Logger : public Logger_IF
{
    public:

        void log( string message )
        {
            this->out( message );
        }

    protected:

        void out( string message )
        {
            cout << "LOG: " << message << endl;
        }
};

class SpecialLogger_IF : public Logger
{
    public:

        virtual void log( string message, string extra ) = 0;
        virtual ~SpecialLogger_IF() {};
};

class SpecialLogger : public SpecialLogger_IF
{
    public:     
        void log( string message, string extra )
        {
            this->out( message );
            this->extra( extra );
        }

    private:

        void extra( string extra )
        {
            cout << "^ EXTRA: " << extra << endl;
        }
};

int main( int argc, char *argv[] )
{
    Logger_IF* logger = new Logger();
    logger->log( "message" );
    delete logger;

    SpecialLogger_IF* spLogger = new SpecialLogger();
    spLogger->log( "message" );
    spLogger->log( "message", "extra" );
    delete spLogger;
}

and I get the following error:

testing.cpp:62:27: error: too few arguments to function call, expected 2, have 1
        spLogger->log( "message" );
        ~~~~~~~~~~~~~            ^
testing.cpp:34:3: note: 'log' declared here
                virtual void log( string message, string extra ) = 0;
                ^
1 error generated.

Interestingly calling this->out( message ) inside void log( string message, string extra ) does work so some inheritance is taking place.

Is what I'm doing correct and possible, or am I missing something.

Regards

Al

Alastair
  • 83
  • 8
  • possible duplicate of [Overloading a virtual function in a child class](http://stackoverflow.com/questions/8816794/overloading-a-virtual-function-in-a-child-class) – Shafik Yaghmour Jul 03 '13 at 16:45

2 Answers2

9

Your problem is that your two parameter function hides the one parameter version in the base class (this is intentional by language design because it prevents more problems than it causes).

Luckily you can fix that with using: Right below the declaration of your two parameter log function just write using Logger::log;.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

As you do, I was looking through the gcc man pages and there is handy warning for this exact issue:

-Woverloaded-virtual

it returned

test.cpp:17:14: warning: ‘virtual void Logger::log(std::string)’ was hidden [-Woverloaded-virtual]
     void log( string message )
          ^
test.cpp:34:22: warning:   by ‘virtual void SpecialLogger_IF::log(std::string, std::string)’ [-Woverloaded-virtual]
     virtual void log( string message, string extra ) = 0;
                  ^

on the above code.

Al

Alastair
  • 83
  • 8