I have a class:
class Base;
Also I have an interface
class Interface;
Next i’m creating a class
class Derived : public Base, public Interface;
If I have Base *object = new Derived;
How can i cast object to Interface ? (of course if i know than object is actually a derived class)
EDIT:
I’ve tried dynamic_cast and static_cast (not compiled).
So let me explain the problem a bit more:
I have:
class Object {...}
class ITouchResponder
{
public:
virtual bool onTouchBegan(XTouch *touch) = 0;
virtual void onTouchMoved(XTouch *touch) = 0;
virtual void onTouchEnded(XTouch *touch) = 0;
};
class Ball : public Object, public ITouchResponder {...};
class TentacleSensor : public Object, public ITouchResponder {...}
Object have a bool touchable_ property. If it’s true then object is implementing ITouchResponder interface.
When I use it:
bool Level::onTouchBegan(XTouch *touch)
{
...
ITouchResponder *responder = callback.nearestTouchable();
if (responder)
{
if (responder->onTouchBegan(touch))
{
if (responder != ball_)
{
touch->setUserData(responder);
}
}
}
return true;
}
ITouchResponder *QueryCallback::nearestTouchable() const
{
for (list<Object*>::const_iterator it = objects_.begin(); it != objects_.end(); ++it)
{
if ( (*it)->isTouchable() ) return (*it)->asTouchResponder();
}
return 0;
}
asTouchResponder is a method of Object :
ITouchResponder * Object::asTouchResponder()
{
assert(touchable_);
ITouchResponder *res = dynamic_cast<ITouchResponder*>(this);
assert(res);
return res;
}
I have bad excess error in xcode.
But if i make Object : public ITouchResponder everything works fine. What am i doing wrong ?
Full object class:
class Object// : public ITouchResponder
{
public:
struct Def
{
Def()
{
level = 0;
world = 0;
touchable = false;
acceptsContacts = false;
body = 0;
node = 0;
}
Level *level;
b2World *world;
bool touchable;
bool acceptsContacts;
b2Body *body;
XNode *node;
};
Object(const Def &def);
virtual ~Object();
virtual void update(float dt);
bool isTouchable() const {return touchable_;}
void addDependantObject(Object *object);
void removeDependantObject(Object *object);
virtual void objectWillBeRemoved(Object *object) {} //this function is automatically called to every dependant object when object is removed
virtual XVec2 position() const;
virtual float rotation() const;
bool acceptsContacts() const {return acceptsContacts_;}
b2Body *body() const {return body_;}
Level *level() const {return level_;}
b2World *world() const {return world_;}
ITouchResponder *asTouchResponder();
/*
virtual bool onTouchBegan(XTouch *touch) {
return false;
}
virtual void onTouchMoved(XTouch *touch)
{
}
virtual void onTouchEnded(XTouch *touch)
{
}*/
protected:
Level *level_;
b2World *world_;
bool touchable_;
bool acceptsContacts_;
XNode *node_;
b2Body *body_;
list<Object*> dependantObjects_;
};
If
Basehasvirtualfunction (even be itvirtualdestructor), then:Else, use
Note that if
Basedoesn’t have virtual function, thendynamic_castwill NOT compile. Indynamic_cast, only the source has to be a polymorphic object, in order to compile, and if the destination isn’t polymorphic, then dynamic_cast will return null pointer:Suppose
AandBare polymorphic type, andCis non-polymorphic, then