Deriving from multiple classes derived from wxEvtHandler

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
4ggre5510n
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sat Jan 14, 2012 10:34 pm

Deriving from multiple classes derived from wxEvtHandler

Post by 4ggre5510n » Thu Apr 26, 2012 8:13 pm

Greetings!

I create my class ( wxMyPanel ) derived both from
wxPanel
wxMouseEventsManager

Every one of those classes derives from wxEvtHandler.

Then, while invoking Bind( wxEVT_PAINT, ...... ) in class constructor, I get one of the errors:
"Reference to 'Bind' is ambigious" ( when I call this->Bind() )
" 'wxEvtHandler' is an ambiguous base of 'wxMyPanel' " ( when I call either wxPanel::Bind() or wxMouEseventsManager::Bind() )

Is there any way to derive both from wxPanel and wxMouseEventsManager ?
How should I do it?

Thanks in advance!
Best regards!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2650
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deriving from multiple classes derived from wxEvtHandler

Post by PB » Thu Apr 26, 2012 8:34 pm

I'm not really sure that what you're trying to do is a good idea. Anyway, you could try two things:
1. use virtual inheritance, e.g. "class wxMyPanel : public wxPanel, public virtual wxMouseEventsManager"
2. and/or qualify the call with the base class name, e.g. wxPanel::Bind(...) or maybe use "using", e.g. "using wxPanel::Bind" in the wxMyPanel declaration.

But I have never done such things myself, so I may be very wrong here. Let's see what doublemax has to say. :)

User avatar
doublemax
Moderator
Moderator
Posts: 15260
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deriving from multiple classes derived from wxEvtHandler

Post by doublemax » Thu Apr 26, 2012 9:57 pm

http://en.wikipedia.org/wiki/Diamond_problem

I always try to avoid situations like this, so i can't offer a clean solution either. There is a good reason that Java doesn't support multiple inheritance, only interfaces.

Try resolving the ambiguities by calling the method of the base class like PB wrote.

If you want to avoid this situation, you could also make wxMouseEventsManager a member of wxMyPanel, even if it makes communication between the two classes less comfortable.
Use the source, Luke!

4ggre5510n
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sat Jan 14, 2012 10:34 pm

Re: Deriving from multiple classes derived from wxEvtHandler

Post by 4ggre5510n » Fri Apr 27, 2012 12:06 am

Thanks so much!

PB:
virtual keyword + qualifying call with class name works perfectly.
But why this could be a wrong idea? I create myPanel, to draw directly on it's DC some really complicated, movable stuff ( not enough complicated for using GLUT thou ). I guess it's the right solution... This 'stuff' is, well, huge image, created from smaller images ( PNG files ), drawn on the fly based on which part user is currently displaying. Something like map of the sky, or huge panorama in HD. Any thoughts about better solution for displaying would be appreciated :)

doublemax:
The communication would be truly nightmarish, hence this topic :)

Solved 100%, can be closed.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2650
Joined: Sun Jan 03, 2010 5:45 pm

Re: Deriving from multiple classes derived from wxEvtHandler

Post by PB » Fri Apr 27, 2012 5:28 am

4ggre5510n wrote:But why this could be a wrong idea?
Well, for me, the reason would be that I am not very good with C++ so I try to keep things as simple as I can, to minimize the possibility of messing-up. Regarding wxMouseEvents, just as doublemax suggested, I would probably go for composition instead of inheritance, but I know nothing of the class, so if you believe passing events around with your wxMyPanel could be complicated and virtual inheritance works...
4ggre5510n wrote:Solved 100%, can be closed.
You are supposed to do that yourself, by clicking the "Solved" button on top of the post that helped you most.

4ggre5510n
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sat Jan 14, 2012 10:34 pm

Re: Deriving from multiple classes derived from wxEvtHandler

Post by 4ggre5510n » Fri Apr 27, 2012 3:36 pm

Hmm, apparently I was a little too eager while reporting this solution solved problem...

Reading through C++FAQ I've found that virtual class inheritance will work only if base classes will derive from same class as virtual.
As ( much better ) explained here:
http://www.parashift.com/c++-faq-lite/m ... l#faq-25.9

So declaring wxMyPanel inheritance as virtual won't give me anything :(
Compilator returns error in convertible.h (template using dynamic_cast to determinate class hierarchy ) saying:
"'wxEvtHandler' is an ambiguous base of 'wxMyPanel'"

And I cannot declare as virtual inheritance in neither wxWindow or wxMouseEventsManager ( as proposed in C++FAQ ) - it severely messes up some other inheritance aspects I don't even exact understand :) [ anyway, it would require recompilation of whole library so it's kinda not worth it ] )

ATM I'm forced to use composition. :| Unless I'm doing something wrong with this virtual inheritance .

BTW:
Funny thing - wxMouseEventsManager is constantly connected with wxWindow ( it doesn't work if it isn't ) -> which already inherits from wxEvtHandler.
It cannot however use wxWindow's evtHandler because it uses EVT_TABLE macros. Without using dynamic events binding, it requires to derive from wxEvtHandler itself, which cause whole this commotion ( or maybe I still misunderstand how evtHandlers work ).

Post Reply