Jump to content
  • Advertisement
Sign in to follow this  
sipickles

Testing for inheritance

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

Quote:
Original post by sipickles
So I gather I should look at other approaches. By this we mean nested classes right?


Not "nested" (which implies nesting of the actual class definitions) but "composed" or "aggregated".

Share this post


Link to post
Share on other sites
Advertisement
Quote:
to test if a derived object is derived from a particular base object


WHAT? You're all recommending dynamic_cast? A derived object can ALWAYS be cast to a base object. That's an OO fundamental.

If you're asking whether a random object can be cast to a random base, then you're
doing something wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
Quote:
to test if a derived object is derived from a particular base object


WHAT? You're all recommending dynamic_cast? A derived object can ALWAYS be cast to a base object. That's an OO fundamental.


struct A {};
struct B {};
struct C : A, B {};

A * a = new C;
B * b = a; // compile error.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nitage
If you're asking whether a random object can be cast to a random base, then you're doing something wrong.


I was actually asking if I could test whether an object (derived through MI from several bases) was derived from a particular base, ie, Is object 'tree', selectable? (has it derived from base object SelectableObject, and therefore possesses appropiate attributes?)

Thanks for all the advice, but I confess to being a little confused. Composition doesn't seem to fit my requirements.

Lode seemed to support my MI theory. I want to be able to have a general list of local game objects, something like this:


class BaseObject
{
// pos, mesh etc
};
class MobileObject
{
// speed, turn(), move() etc
};


class Rock : public BaseObject {};
class FastShip : public BaseObject, public MobileObject {}

std::map< int, boost::shared_ptr<BaseObject> > inGameObjects;

boost::shared_ptr<Rock> boringRock = boost::shared_ptr<Rock>(new Rock);
boost::shared_ptr<FastShip> pirate = boost::shared_ptr<FastShip>(new FastShip);

inGameObjects[1003] = boringRock;
inGameObjects[22] = pirate;

////

std::map< int, boost::shared_ptr<BaseObject> >::iterator iter;

for ( iter = inGameObjects.begin(); iter != inGameObjects.end(); iter++ )
if ( boost::is_base_of<MobileObject, iter.second> == boost::true_type )
iter.second->move();






Go on then... do your worst! ;)

Share this post


Link to post
Share on other sites
Ah, I think I have it.....

Instead I want something like this:


class CommonProperties
{
// pos, mesh etc
};
class MobileProperties
{
// speed, turn(), move() etc
};


class GameObject
{
public:
GameObject() { m_common = boost::shared_ptr<CommonProperties>( new CommonProperties ); }

MakeMobile() { m_mobile = boost::shared_ptr<MobileProperties>( new MobileProperties ); }
boost::shared_ptr<MobileProperties> IsMobile() { return m_mobile; }


private:
boost::shared_ptr<CommonProperties> m_common;
boost::shared_ptr<MobileProperties> m_mobile;
};


std::map< int, boost::shared_ptr<GameObject> > inGameObjects;

boost::shared_ptr<GameObject> boringRock = boost::shared_ptr<GameObject>(new GameObject);

boost::shared_ptr<GameObject> pirate = boost::shared_ptr<GameObject>(new GameObject);
pirate->MakeMobile();

inGameObjects[1003] = boringRock;
inGameObjects[22] = pirate;

////

std::map< int, boost::shared_ptr<GameObject> >::iterator iter;

for ( iter = inGameObjects.begin(); iter != inGameObjects.end(); iter++ )
if ( iter.second->IsMobile() )
iter.second->move();





Any better?

Should I be using weak_ptrs as return types from IsMobile()?

Thanks

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!