Sign in to follow this  

Problem with multiple inheritance

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

This topic is 4675 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this