Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ multiple inheritance problems

Started by MajinMusashi Aug 9, 2005 at 12:41 AM 4 replies 500+ views
Original Post
MajinMusashi
MajinMusashi
Hello! I'm using Visual C++ 6.0, and I'm having some strange problems using C++ multiple inheritance and virtual base classes, and hope you can help me (I'm totally confused). See the example below:

// element.hpp - Virtual base class (element)
class C_Element {
public:
    virtual void draw() = 0;
    ...
}

// eventhandler.hpp - Virtual base class (event handler)
class C_EventHandler {
public:
    virtual void onMouseClick();
    ...
};

// keyhandler.hpp - Virtual base class (keyboard event handler)
class C_KeyHandler : public C_EventHandler {
public:
    virtual void onKeyPress( unsigned int );
    ...
};

// el_window.hpp - GUI window class
class C_Window : public C_Element, public C_KeyHandler {
    std::vector<C_Element*> elemVector;	// Vector of GUI elements (buttons, 
    ...
};

// el_button.hpp - GUI button class
class C_Button : public C_Element, public C_KeyHandler {
    ...
}


The problem: when I try to access the some "C_EventHandler" method (eg. "onMouseClick()") from within "C_Window" implementation file, a compiler error is thrown.

// el_window.cpp - GUI window implementation file
void C_Window::test() {
    cout << "Mouse click detected";
    elemVector[elemNr]->onMouseClick();	//   <--- ERROR HERE
}

// ERROR: el_window.cpp(226) : error C2039: 'onMouseClick' :
//        is not a member of 'C_Element' : see declaration of 'C_Element'


I have googled for multiple inheritance and virtual base classes and something but found nothing about. Can you show me the correct way to do that? Thanks a lot!
Kristafarii
Kristafarii
Is the onMouseClick declared with public or protected access?
If undefined it will be assumed private
Sneftel
Sneftel
Kris, access specifiers are not the problem here; they wouldn't generate that error message.

Majin, elemVector is declared as a vector of C_Element*s, and C_Element does not have onMouseClick. Consider: suppose one of the C_Element*s in the vector pointed to a subclass of C_Element that did not also inherit from C_EventHandler. What would that call do? Nothing would be appropriate.

You could use dynamic_cast here to cast to a C_EventHandler*, and if the returned pointer is not null, call onMouseClick on it. You should consider refactoring, however. Would it make sense to put onMouseClick into C_Element, perhaps with a default behavior that simply ignored the event?
MajinMusashi
MajinMusashi
Quote:
Original post by Kristafarii
Is the onMouseClick declared with public or protected access?
If undefined it will be assumed private

Kristafarri, I think that doesn't matter by now, since the compiler error has nothing to do with access permissions but with reaching the base class. Anyway, I'll edit my example to make it clear that "onMouseClick" is declared "public".

Thanks!
Kristafarii
Kristafarii
sorry, read the code not the error,
although it looks like the code's been updated..
MajinMusashi
MajinMusashi
Quote:
Original post by Sneftel
Majin, elemVector is declared as a vector of C_Element*s, and C_Element does not have onMouseClick. Consider: suppose one of the C_Element*s in the vector pointed to a subclass of C_Element that did not also inherit from C_EventHandler. What would that call do? Nothing would be appropriate.

Yes, I understand it now :)

Quote:
You could use dynamic_cast here to cast to a C_EventHandler*, and if the returned pointer is not null, call onMouseClick on it. You should consider refactoring, however. Would it make sense to put onMouseClick into C_Element, perhaps with a default behavior that simply ignored the event?

Yes again, I'm refactoring my classes. In fact, "C_EventHandler" was merged into "C_Element" since every element does basic event handling.

Thanks a lot!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.