Sign in to follow this  
GeekPlusPlus

Classes with two parents

Recommended Posts

GeekPlusPlus    124
Hey, If I have a class inheriting from two parents, is there a way I can define an overload function in the second parent of a function in the first parent? e.g.
class CClassOne
{
public:
	void CFunction(void);
};

class CClassTwo
{
public:
	void CFunction(void);
};

class CClassThree: public CClassOne, public CClassTwo
{
public:
};
See, I have objects that inherit from just ClassOne, and objects that inherit from ClassOne and ClassTwo. Originally I was going to just virtual the function in ClassOne and override it in each of the children that also inherit from ClassTwo. But realized the functions of the children that inherit from ClassTwo will also all be the same, breaking the once and only once rule I love so much. Can I somehow tell it which is "more important"? Any ideas?

Share this post


Link to post
Share on other sites
cavemanbob    152
No.

EDIT: having reread it you may be looking for a way to bypass a diamond inheritance pattern, in which case it's really no and you should look at a better layout for your base classes.

Share this post


Link to post
Share on other sites
joanusdmentia    1060
Quote:
I have objects that inherit from just ClassOne

Hmm...not in the source you listed. Is CClassTwo meant to be inheriting from CClassOne?

If the answer is no, then what you have is called multiple inheritance and should generally be avoided, especially if you've only just started with C++. It does have it's place but it's very easy to mess up.

However if the answer is yes, then CClassThree doesn't need to inheret from CClassOne, it will automatically get everything from CClassOne through CClassTwo. So you can do it like this (notice that you don't need to specify void in the parameters):


class CClassOne
{
public:
virtual void CFunction();
};

class CClassTwo : public CClassOne
{
public:
// Overrides CFunction() from CClassOne
virtual void CFunction();
};

class CClassThree: public CClassTwo
{
public:
// Overrides CFunction() from CClassTwo
virtual void CFunction();
};


Now, from within CClassThree::CFunction() you can call the CFunction() from your base classes, that is CClassTwo::CFunction() and CClassOne::CFunction like follows:


void CClassOne::CFunction()
{
// Do something
}

void CClassTwo::CFunction()
{
CClassOne::CFunction();
// Do something extra
}

void CClassThree::CFunction()
{
CClassTwo::CFunction();
// Do something extra
}


Hope that helps!

Share this post


Link to post
Share on other sites
GeekPlusPlus    124
No, it's not a diamond, and no two doens't inherit from 1.
And I believe my situation warents the multiple inheritence.

It's owners (or containers, entities that OWN things). owners have very specialized features, a list of things it owns and methods of aquireing them and releasing them. class one is an entity item. two is the owner class

so class three is an entity of some sort which can contain things.

The reason I don't do them inline is because, well look at this inheritence model...


Entity
|- Item
| |- Weapon
| |- Container
|- Mobile
|- Player
|- npc


See all mobiles would be owners, but only containers would be owners in the item category. So you see how it gets messy.

Mobile->Owner->Entity
Container->Owner->Entity
\->Item->Entity?!

Doesn't work at all. =/

Also the world has a list of entities, it owns them, so it'd be an owner as well and not even an entity!

But either way, since there IS no way to define it. I assume my best bet is to just define each function in the owning children. Not so horrible.

And yes i've found some of the "not for newb" perks of multiple inheritence, like you really have to watch your type casting.

If you've got any suggestions i'm open to them. =)

Thanks,

Share this post


Link to post
Share on other sites
Hellsp4wn    128
Do you really have to call the two functions the same? If it's not really necessary, you have the solution right there. Maybe you already thought about that, but still...

Share this post


Link to post
Share on other sites
GeekPlusPlus    124
It's a good point none the less, but I have thought of that. It's the same function really, but the owners have to be treated differently than non owners. Removing the entity from the world for example. Have to handle owner property if it has any, where non owners you don't. There's some others. But with that said...

I don't HAVE to, but that would leave the "other" function as a viable option for the wrong type. If you ever used it... Well only god can predict what would happen. =)

Actually right now I have a large case that says this type do this if that type do that, and if I left it in I could do that. But it just seems icky. Ya know?

Share this post


Link to post
Share on other sites
Jingo    582
Hi GeekPlusPlus.

You can specify which function is called by using a using declarator in CClassThree, this code demonstrates it.


class CClassOne
{
public:
void CFunction(void);
};

class CClassTwo
{
public:
void CFunction(void);
};

class CClassThree: public CClassOne, public CClassTwo
{
public:
using CClassTwo::CFunction;
};



Hope that helps.

Share this post


Link to post
Share on other sites

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