Jump to content
  • Advertisement

Archived

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

siddim

recasting?

This topic is 5653 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, i wanna to do some sort of objects manager, object can herit those interface: IDrawable, IMessageable and IObject. so now, all object are in a list that hold pointer to the IObject interface. Now in my Object manager i want to build other lists of pointer to IDrawable and IMessageable, but i only have the pointer to the IObject interface plus i know if the object in question implement one or two of the other interface. can i just recast it ? class a: public IMessageable, public IObject, public IDrawable { ... }; now in the objectManager i have a list of pointers to IObject. i want to build another list with all the IMessageable object: IMessageableList.add((IMessageable*)pObject); it did''n seem to be working? thanks. siddim.

Share this post


Link to post
Share on other sites
Advertisement
quote:

IMessageableList.add((IMessageable*)pObject);



If pObject points to an object whose type is derived from IMessageable, then you don''t have to cast it.

If you don''t know if pObject is derived from IMessageable, then dynamic_cast might work (I''m not sure about this).

You might try this:

IMessageable * pM = dynamic_cast< IMessageable * >( pObject );
if ( pM != 0 )
{
IMessageableList.add( pM );
}

Share this post


Link to post
Share on other sites
it did'n seem to work,

here's the situation

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

in the code i have:

D* pD = new D();
function(pD);
...



void function(A* pA_)
{
pB = (B*)pA_; // this is my problem, i want to change the pointer so that it point to another interface.
}

thanks



[edited by - siddim on April 2, 2003 2:55:46 PM]

Share this post


Link to post
Share on other sites
dynamic_cast should work. A c-style cast might work, sometimes, somewhere, I don''t know. But probably not.

dynamic_cast only works with polymorphic objects (i.e. have a virtual member) and RTTI must be enabled. If your classes, A, B, and C, doesn''t have a virtual member, dynamic_cast would have given you a warning (''X is not a polymorphic type'', or something).

C-style casts are bad

Regards Mats

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sounds like a tricky question!

Share this post


Link to post
Share on other sites
I had the same problem and solved it like this: I gave each interface a constructor and a destructor that add/remove the object from the list. Link: http://www.gamedev.net/community/forums/topic.asp?topic_id=148076

[edited by - Hazelnuss on April 3, 2003 9:33:16 AM]

[edited by - Hazelnuss on April 3, 2003 9:33:42 AM]

Share this post


Link to post
Share on other sites
Don''t know if this''ll work for you, but how about not using inheritance but aggregation? I mean, let the object have a reference to IDrawable and IMessageable as a data member.

Share this post


Link to post
Share on other sites
quote:
void function(A* pA_)
{
pB = (B*)pA_; // this is my problem, i want to change the pointer so that it point to another interface.
}


Your problem, as you rightly say, is that you want to cast the pointer. This probably means something has gone wrong with your design.

Your mileage will vary depending on the exact details of what you are doing but...

The function operating on the object of type A then checks to see if the object is also of Type B and does something if it is. This smells really bad... you're switching behaviour based on object type... that's what polymorphism came to save us from.

If you move that function onto a class that A and B inherit from and they override some virtual function to do their specific stuff you wouldn't have this problem.

Just a suggestion.

Pete

[edited by - petewood on April 3, 2003 10:30:53 AM]

Share this post


Link to post
Share on other sites
It''s called a cross cast. dynamic_cast will do it, as long as A and B share the complete object D. And, as stated earlier, they must be polymorphic.

This might help

  
#include <iostream>

struct A {
virtual void a() {std::cout << "a" << std::endl;};
};

struct B {
virtual void b() {std::cout << "b" << std::endl;};
};

struct C {
virtual void c() {std::cout << "c" << std::endl;};
};

struct D : public A, public B, public C
{
virtual ~D() {}
};

int main()
{
using std::cout;
using std::endl;

D *d = new D;

A *a = d; // implicit up cast, ok

a->a();

B *b = (B*)a; // will fail -> evil

b = static_cast<D*>(a); // through derived and implicit up cast, ok

b = dynamic_cast<B*>(a); // cross cast from A to B, ok

if ( b == 0 )
cout << "failed" << endl;
else
b->b();

C *c = dynamic_cast<C*>(a); // cross cast from A to C, ok

if ( c == 0 )
cout << "failed" << endl;
else
c->c();

delete d;

return 0;
}


Regards Mats

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!