Jump to content
  • Advertisement
Sign in to follow this  
Muncher

inheritance question

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

I have an inheritance problem and im not sure how to resolve it; The only way i can explain it is with an example... Basically i have a Base class with a member variable m_DEF; The classes B1 and B2 inherit from the base. The class derived inherits from both B1 AND B2 So if i create an object of type derived, and try to access Derived->m_DEF i get the error "error C2385: ambiguous access of 'm_DEF' in 'Derived'" To me that makes sense (please correct me if im wrong) because there are 3 m_DEF's; one in the B1::m_DEF, the B2::m_DEF and the Base::m_DEF. The question: is there a way to make the Base::m_DEF be the ONLY one? I don't want multiple copies of m_DEF - or, is it possible for the value in Base::m_DEF to overwrite the values of all child classes? Thanks for any ideas :) Muncher
// Base class
class Base
{
public:
	Base(){};
	~Base(){};

	std::string m_DEF;
};

// inherits from Base
class B1 : public Base
{
public:
	B1(){};
	~B1(){};

	int b1;
};

// also inherits from base
class B2 : public Base
{
public:
	B2(){};
	~B2(){};

	int b2;
};

// inherits from B1 and B2 AND Base
class Derived : public B1,
		public B2,
                public Base
{
public:
	Derived() {};
	~Derived(){};

	int derived;
};


int main()
{
	Derived* p = new Derived();
	p->m_DEF = "Hello";          // Error, ambiguous access
}

Share this post


Link to post
Share on other sites
Advertisement
Genuinely not sure if I understand this question, but I think you may be looking for virtual inheritance.

This is basically meant to ensure that, in cases where multiple base classes are inherited from, only a single instance of the base is created.

Multiple inheritance is trouble, I tell ya.

Share this post


Link to post
Share on other sites
This kind of problem is the reason that most people avoid multiple inhertance. Try not to have 2 base classes that share the same super class.

I'm not infront of c++ compiler so I can't this... but try

B1::p->m_DEF = "Hello";

or

B2::p->m_DEF = "Hello";

Share this post


Link to post
Share on other sites
thanks for the help, there are a whole bunch of questions i could ask :) but i think i should do some reading instead.

I have made some changes, are they ok?

Muncher



// Base class
class Base
{
public:
Base(){};
~Base(){};

std::string m_DEF;
std::string m_USE;
};

// derives from Base
class B1 : virtual public Base
{
public:
B1(){};
~B1(){};

int b1;
};

// also derives from base
class B2 : virtual public Base
{
public:
B2(){};
~B2(){};

int b2;
};

// derives from B1 + B2
class Derived :
public B1,
public B2
{
public:
Derived() {
Base::m_DEF = "Timmy";
};
~Derived(){};

int derived;
};


int main()
{
Derived* p = new Derived();

p->m_DEF = "Hello"; // I get no error now...
}


Share this post


Link to post
Share on other sites
Quote:
or, is it possible for the value in Base::m_DEF to overwrite the values of all child classes?


Don't quote me on this (pun intended), but I'm not *entirely* sure if that fits in so well with the concept of inheritance. I believe that in a perfect child/parent relationship, the child *always* retains the right to behave differently from the parent (i.e. "this is impossible").

If I'm wrong, someone will correct me, I'm sure.

Share this post


Link to post
Share on other sites
"To me that makes sense (please correct me if im wrong) because there are 3 m_DEF's; one in the B1::m_DEF, the B2::m_DEF and the Base::m_DEF."

Nope, there are two. In a simpler inheritance scheme, where you have Base and Derived, B::member is the same as D::member.

Lastly, are you really, really, sure that you want to use multiple inheritance? I rarely even use regular inheritance and when I do I almost never go beyond one or two levels deep.

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!