Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

aboeing

Muliple Inheritance Ordering (c++)

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

If you are doing evil things with MI and casting, is it gauranteed that you can cast an object with MI to the first object it is inherited from and get the right object? ie:
class Base {
};
class One: public Base {
};
class Two {
};
class MI : public One, Public Two {
};

//in the main code:

Base *pBase = (Base *) new MI;
//okay can call all pBase functions fine

One *pOne = (One *) pBase;
//can call all One functions fine??



And if I swap the position of Two and One, ie:
   
class MI : public Two, public One {};
Two *pTwo = (Two *) pBase;
//can call all Two functions fine??

It seems to work with VC6, but im not sure if the laying out in memory in that order is a standard or implementation specific. Compilers an OS''s im interested in are MS, GCC, Intel, windows,linux,solaris. PS: sorry to anyone who read this before i accendentially posted to the wrong forum

Share this post


Link to post
Share on other sites
Advertisement
The casts should work no matter what order the base classes are specified in the inheritence due to delta artihmetic done by the cast operators. However, I''d change the C style cast to a static_cast and throw in a assertion that the equivalent dynamic_cast yields the same pointer that the static_cast did. Just be careful not to ever change any inheritance in that hierarchy to virtual and make sure that the casts never take place when you only have forward declarations to the class types involved.

That being said, the fact that you''re doing this is probably indicative of a design flaw in your class hierarchy.

Share this post


Link to post
Share on other sites
The answer to your question is yes. Regardless of layout in memory, an object can always be implicitly converted to any of its base classes. If you have

class C : public A, public B
{
...
};

then you can do this:

A *base = new C;

or:

B *base = new C;

When you have a pointer to an object of type A or B that you know for sure is really an object of type C, then you can get a pointer to C back by doing the following:

C *derived = static_cast<C *>(base);

DO NOT use the the C-style cast like this:

C *derived = (C *) base;

The reason is that the C-style cast will compile successfully even if the class C isn't completely defined (that is, you forward-declared C somewhere, but didn't define it yet). If you're casting from a base class that doesn't come first in the structure of the class C, then the compiler won't do the address adjustment. Using static_cast requires that the class you're casting to is completely defined, so you're always safe because the compiler will complain if you use it with a forward-declared class that hasn't been defined.

[edited by - Eric Lengyel on June 3, 2004 3:55:45 AM]

Share this post


Link to post
Share on other sites
Thanks for the replies, I think I might have confused you as to whats going on here.
I have a factory creating base, factory objects.
This is what I get a pointer to, and I need to convert it to the top level class correctly.

Diagram:
X FactoryBase
| |
MI Object

Trying to do a dynamic cast will fail in this instance.

All in all, I think SiCrane is right in saying my design is bad. (Ive been thinking it sucked for a while now.)
I'll draw my design which I am now planning instead:

Base Factory Object
|
Body
| | C1Body
IBox /
| /
C1Box

And using VMI.

Previously I was trying:
Body
|
IBox < template >

with:
Body
|
C1Body

IBox < C1Body > BaseFactoryObject
| |
C1Box

And I also moved the BaseFactoryObject to be inherited by 'Body', but would still run into problems with dynamic casting because of the template inheritance - and wasn't sure if this is safe anyway??

Also, as far as I know, I cant use the bridge pattern here, because there are many different concrete implementations, ie: C2Body,C2Box,C3Body,C3Box,etc. and the 'C1Box' relies on both the IBox and C1Body.... (something like a doublebridge perhaps?)

I'd definately welcome any suggestions.
Cheers.

edit: damn templates. >=/

[edited by - aboeing on June 3, 2004 4:30:19 AM]

Share this post


Link to post
Share on other sites

  • 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!