Sign in to follow this  
derek7

OMG I can not cast base pointer to derived pointer?

Recommended Posts

Movable *t = g_mgr.entity("arth"); //c2440 error Entity derive from Movable , entity() return a Entity pointer some relatived code:

typedef std::map<std::string, Entity* > EntityList;
EntityList m_entityList;


Entity* SceneManager::entity(const std::string& name)
{
    EntityList::iterator i = m_entityList.find(name);
    if (i == m_entityList.end())
    {
		return(Entity*)0;
       //except
    }
    else
    {
        return i->second;
    }
}

Share this post


Link to post
Share on other sites
generally speaking, casting to a base class is always possible... but you're trying to cast a base class to a derived class. Haven't actually done that myself, but it seems logically valid that it would fail.

casting to base class is valid, however, because the base class in its entirety is a part of the derived class. casting to a derived class however is not recommended (if not illegal in the first place ). Imagine:


class A
{
// Simplified for brevity

public:
virtual int DoSomething();
};

class B: public A
{
public:
virtual int DoSomething();
virtual int DoSomethingElse();
};

// USAGE possiblities?
A* toBase = dynamic_cast<A*>( new B() );
toBase->DoSomething();
//toBase->DoSomethingElse(); <-- This will give compile time error

B* toDerived = dynamic_cast<B*>( new A() ); // Don't think this will work

// Why? Because the following would cause a runtime error if the above
// succeeded. A pointer to B will allow access to a defined 'DoSomethingElse'
// which is non-existent in the original class-A
toDerived->DoSomethingElse();



not sure if my explanation helps, but thats my 2cents.

Share this post


Link to post
Share on other sites
SceneManager::entity is returning an Entity*, the base class. Even if it is a Movable, you have to cast it. To do so, you use dynamic_cast<Movable*>(g_mgr.entity("arth"));.

Note that if it isn't the derived class, dynamic_cast returns 0 at runtime, so you should check for null unless you're 105% sure that it will be what you're casting to, in which case you can use reinterprit_cast<Movable*>(entity) or (Movable*)entity, which won't do any type checking.

Now if you want to cast down from a derived to base class you can use static_cast<Entity*>(Movable), and it will do compile time checks instead of runtime checks.

If you don't know this, you should really go out and buy a good book for beginners in C++.

Crazee:
B* toDerived = dynamic_cast<B*>( new A() );
will compile just fine, it will check it at run time and toDerived will equal 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by CraZeE
generally speaking, casting to a base class is always possible... but you're trying to cast a base class to a derived class. Haven't actually done that myself, but it seems logically valid that it would fail.

You can, and the compiler will probably let you, but if it isn't a valid class the results are undefined. dynamic_cast was added specifically to cause such an invalid cast to explicitly fail, whether it be an upcast [base to derived] or a down cast [derived to base].

CM

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