c ++多继承转换 [英] c++ multiple inheritance casting

查看:161
本文介绍了c ++多继承转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:

class Base;

也有一个接口

class Interface;

接下来我要创建一个类

class Derived : public Base, public Interface;

如果我有 Base * object = new Derived;

如何将对象强制转换为接口 ? (当然如果我知道对象实际上是一个派生类)

How can i cast object to Interface ? (of course if i know than object is actually a derived class)

EDIT:

我试过dynamic_cast和static_cast(未编译)。
因此,让我更多地解释这个问题:

I've tried dynamic_cast and static_cast (not compiled). So let me explain the problem a bit more:

我有:

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 {...}

对象有一个 bool touchable _ 属性。

当我使用它时:

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 方法对象

    ITouchResponder * Object::asTouchResponder()
    {
        assert(touchable_);
        ITouchResponder *res = dynamic_cast<ITouchResponder*>(this);
        assert(res);
        return res;
    }

我在xcode中有多余的错误。

I have bad excess error in xcode.

但如果我使 Object:public ITouchResponder 一切正常。我做错了什么?

But if i make Object : public ITouchResponder everything works fine. What am i doing wrong ?

完整对象类:

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_;
};


推荐答案

如果 Base virtual 函数(即使是 virtual 析构函数),然后:

If Base has virtual function (even be it virtual destructor), then:

Derived *pDerived = dynamic_cast<Derived *>(object);

另外,使用

Derived *pDerived = static_cast<Derived *>(object);






请注意,如果 Base 没有虚拟函数,则 dynamic_cast 将不会编译。在 dynamic_cast 中,源必须是一个多态对象,以便编译,如果目标不是多态的,dynamic_cast将返回空指针:


Note that if Base doesn't have virtual function, then dynamic_cast will NOT compile. In dynamic_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:

假设 A B 多态性类型, C 是非多态性,则

Suppose A and B are polymorphic type, and C is non-polymorphic, then

A *pA = dynamic_cast<A*>(new C()); //error - source is not polymorphic!

A *pA = dynamic_cast<A*>(new B()); //ok
if ( pA == 0 )
      cout << "pA will be null if B is not derived from A" << endl;

C *pC = dynamic_cast<C*>(new B()); //ok
if ( pC == 0 )
       cout << "pC must be null" << endl;

这篇关于c ++多继承转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆