Jump to content
  • Advertisement
Sign in to follow this  
LonelyStar

Problem with multiple inheritance

This topic is 4922 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

Hi everybody, I have a problem, where a class is derived from 2 base classes which have the same base classes, like in this example:
class BaseClass
{
protected:
  int base_var;
};

class ClassA : public BaseClass
{
protected:
  int a_var;
};

class ClassB : public BaseClass
{
protected:
  int b_var;
};

class LastClass: public ClassA,ClassB
{
public:
  void DoSomething()
  {
    int my_var=base_var;
  }
};

The problem occures when I want to get access to variable of the lowest base class (as in the "int my_var=base_var;" line). The is, what g++ gives me: g++ test2.cc test2.cc: In member function `void LastClass::DoSomething()': test2.cc:24: error: request for member `base_var' is ambiguous in multiple inheritance lattice test2.cc:4: error: candidates are: int BaseClass::base_var test2.cc:4: error: int BaseClass::base_var How can I do it? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
try : virtual public BaseClass

see whats happened is A and B both have their own BaseClass. By declaring it virtual, they share the same base class.

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!