Register mouse events to a base class and receive events on it's sons?

Hi all !

I’ve simple class structure with a base class and some child classes that extends the base .
I would like to add listeners to OF events to the base and then get the events with the extended child classes.
So I did (tried) :

class ClassA 
{
    void setup();

    void mouseMoved(ofMouseEventArgs &a){};
    void mouseDragged(ofMouseEventArgs &a){};
    void mousePressed(ofMouseEventArgs &a){};
    void mouseReleased(ofMouseEventArgs &a){};
}

ClassA::setup()
{
    ofRegisterMouseEvents(this);
}

class ClassB : public ClassA
{
    void mouseMoved(ofMouseEventArgs &a);
    void mouseDragged(ofMouseEventArgs &a);
    void mousePressed(ofMouseEventArgs &a);
    void mouseReleased(ofMouseEventArgs &a);
}

Like this, it compiles but events are not reaching it’s function ClassB::mouseMoved() ?¿
Do i need to add the ofRegisterMouseEvents(this); to the child classes ?
I also tried defining on ClassA mouse related functions as ex :

virtual"as ex (virtual void mouseMoved(ofMouseEventArgs&a))

but then i got a crash on execution ?¿

Can someone light me on how to register events on the base class and get the events from the derived classes ?

Thanks !

How about this?
Add event to superclass’s function via subclass instance

ofAddListener(someEvent, static_cast<SuperClass*>(&subClass), &SuperClass::foo);

Hi,
With virtual functions:

class ClassA
{
public:
    void setup(){
        ofRegisterMouseEvents(this);
    }
    virtual void mouseMoved(ofMouseEventArgs &a){ cout << "A move" << endl; };
    virtual void mouseDragged(ofMouseEventArgs &a){ cout << "A drag" << endl; };
    virtual void mousePressed(ofMouseEventArgs &a){ cout << "A press" << endl; };
    virtual void mouseReleased(ofMouseEventArgs &a){ cout << "A release" << endl; };
};

class ClassB : public ClassA
{
public:
    void mouseMoved(ofMouseEventArgs &a){ cout << "B move" << endl; };
    void mouseDragged(ofMouseEventArgs &a){ cout << "B drag" << endl; };
    void mousePressed(ofMouseEventArgs &a){ cout << "B press" << endl; };
    void mouseReleased(ofMouseEventArgs &a){ cout << "B release" << endl; };
};

in ofApp.h:

ClassA a;
ClassB b;

in ofApp::setup():

a.setup();
b.setup();

src.zip (1.2 KB)

And

a.setup();
ClassA * test = &b;
test->setup();

works too.

Good ! Thanks a lot @lilive !!