0

I'm facing an unexpected behavior using inheritance and overloading. Or rather I'm doing it wrong.

(As suggested in this post) I have a class who respond to a C api using a callback function:

class APIWrapper {
protected:
    // This callback is registered to a C api
    static void _messageCallback(double deltatime, vector< unsigned char > *message, void *userData);

public:
    void manageNewMessage(double deltatime, vector<unsigned char> *message);
};

void APIWrapper::_messageCallback(double deltatime, vector< unsigned char > *message, void *userData) {
    ((APIWrapper*) userData)->manageNewMessage(deltatime, message);
}

And a class which inherit from the first one.

class MyController : public APIWrapper {
public:
    void manageNewMessage(double deltatime, vector<unsigned char> *message) {
        // Customization
        message.at(1) = 0;
        APIWrapper::manageNewMessage(deltatime, message);
    }
};

If I declare a MyController object, the overloaded method MyController::manageNewMessage(double, vector<unsigned char>*) is never called. Is it a normal behaviour?

Community
  • 1
  • 1
T3e
  • 539
  • 1
  • 6
  • 17

1 Answers1

3

I don't see the function being virtual, for your inherited method be able to be dynamically invoked through dynamic binding it must be virtual in the ancestor class.

virtual void manageNewMessage(double deltatime, vector<unsigned char> *message);

In addition, if the method of APIWrapper class shouldn't do anything, then you could declare it as pure virtual:

virtual void manageNewMessage(double deltatime, vector<unsigned char> *message) = 0;

In this way you can avoid declaring even an empty method in the father class, and most important your code won't compile if you forget (or make any mistake in the signature) to override the method. This because all pure virtual methods must be implemented in a child class to be able to allocate new instances of it.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I knew it was simple. So in my case it's only virtual (not pure), thanks for the precision. – T3e May 01 '13 at 22:07