7

I have this code :

class Event{};
class CustomEvent:public Event{};

class Handler
{
  public:
      virtual void inform(Event e ){}
};

class CustomHandler : public Handler
{
  public:
    void inform(CustomEvent e){}        

};

CustomEvent cEvent;
Handler* handler = new CustomHandler;

//this calls Handler::inform(Event), not CustomHandler::(CustomEvent) , as I expected
handler->inform(cEvent); 

If I change the code to this :

class Handler
{
  public:
      virtual void inform(Event e ){}
      virtual void inform(CustomEvent e){}

};

class CustomHandler : public Handler
{
  public:
    void inform(CustomEvent e){}        

};

CustomEvent cEvent;
Handler* handler = new CustomHandler;

//this calls CustomHandler::(CustomEvent) 
handler->inform(cEvent);

I read that this is connected with function overriding and hiding but still doesn't understand the behavior in this code.

user152508
  • 3,053
  • 8
  • 38
  • 58
  • 3
    Many things are wrong with the code. For starters, the function arguments should be *references*. – Kerrek SB Apr 03 '12 at 10:12
  • 2
    Because of (a) slicing and (b) a unnecessary copy. – ipc Apr 03 '12 at 10:22
  • 1
    Haters gonna hate. The question is still valid, even if some don't like unnecessary references. Who's brave enough out of the blue to decide what copy is unnecessary? – Kobor42 Feb 21 '14 at 14:52

1 Answers1

9

Function overloading does not work based on the runtime type of the arguments (which for your argument here is CustomHandler*) but rather on their static type (which here is Handler*, as that's what handler is declared as).

Virtual functions allow you to make function calls based on the runtime type of one object (the one you call the function on). Dispatching calls based on the runtime type of multiple objects is called multiple dispatch; in this instace we 're talking about the most common case of double dispatch. If you want this kind of functionality you are going to have to implement double dispatch or use a library that does it for you.

The Visitor pattern is a pretty common way of doing the implementation; see also Difference betwen Visitor pattern & Double Dispatch.

Finally, you can find a good discussion of Visitor which includes example code (scroll down) here.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • +1 you wrote what i intended to answer. except i planned to include a concrete visitor pattern example. please add that – Cheers and hth. - Alf Apr 03 '12 at 10:19
  • @Cheersandhth.-Alf: Added a link which should do fine. I didn't want to add an example inline (too much boilerplate). – Jon Apr 03 '12 at 10:28