Jump to content
  • Advertisement
Sign in to follow this  
MajinMusashi

C++ multiple inheritance problems

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!