简体   繁体   English

我可以将对象类转换为它实现的接口指针吗?

[英]Can I cast an object class to the interface pointer in which it implements?

I have defined an interface class like the following. 我已经定义了如下所示的接口类。

class IRecyclableObject
{
public:
    virtual ~IRecyclableObject() {}

    virtual void recycle() = 0;
    virtual void dump() = 0;
    virtual int getRecycleTypeID() = 0;
};

The following is my CharacterAI class which inherits another class and implements above interface class. 以下是我的CharacterAI类,它继承了另一个类并实现了上面的接口类。 It's defined as follows. 它的定义如下。

class CharacterAI : public harp::Character, public harp::IRecyclableObject
{
public:
    CharacterAI();
    virtual ~CharacterAI();

    ...

    // -- note these 3 virtual functions -- //
    virtual void recycle();
    virtual void dump();
    virtual int getRecycleTypeID();

    ...
};

Those three virtual functions as defined in interface, I have implemented it normally in CharacterAI class. 接口中定义的那三个虚函数,我已经在CharacterAI类中实现了它。 Nothing fancy goes there. 没有什么花哨的。

It's time when I put them into use with ObjectPool (a self-made) class in which the data storage of available objects in 是时候将它们与ObjectPool (一个自制的)类一起使用,其中可用对象的数据存储在

m_freeList

uses CCArray class. 使用CCArray类。

The problem occurs in the following code. 以下代码中出现此问题。

IRecyclableObject* ObjectPool::popFreeObjectAndAddToActiveListForType(int recycleTypeID)
{
    // search from free-list
    for(unsigned int i=0; i<m_freeList->count(); i++)
    {
        IRecyclableObject* obj = (IRecyclableObject*)m_freeList->objectAtIndex(i);
        CharacterAI *obj1 = (CharacterAI*)m_freeList->objectAtIndex(i);

        CCLog("recycleTypeID : %d %d %d", obj->getRecycleTypeID(), recycleTypeID, obj1->getRecycleTypeID());

        ...
    }
    return NULL;
}

The expected result is to show 预期的结果是显示

recycleTypeID : 4 4 4

But I got 但是我得到了

recycleTypeID : 524241408 4 4

The first one is clearly garbage there and randomly different from each loop. 第一个显然是垃圾,并且每个循环随机不同。 I tried to put a breakpoint in the implemented function getRecycleTypeID() inside CharacterAI class before it returns. 我试图在返回之前在CharacterAI类中的实现函数getRecycleTypeID()中放置一个断点。 I found out that only 我发现只有

obj1->getRecycleTypeID()

was called but not another. 被召唤但不是另一个。

By focusing on obj variable, It's clearly seen that it seems different object calls that function, and the cause may come from casting an object to interface class and use if from there in which it's wrong or some kind. 通过关注obj变量,可以清楚地看到它看起来似乎是不同的对象调用,并且原因可能来自于将对象转换为接口类,并且如果从那里使用它是错误的或某种类型的话。

What's going on there? 那里发生了什么? Can I cast an object type class to interface class pointer (which it implements) and correctly call functions as defined in interface class? 我可以将对象类型类转换为接口类指针(它实现)并正确调用接口类中定义的函数吗?

Can I cast an object class to the interface pointer in which it implements? 我可以将对象类转换为它实现的接口指针吗?

Yes. 是。 But that's not your case. 但那不是你的情况。 Function objectAtIndex() returns a pointer to a CCObject , and that class definitely does not implement the IRecyclableObject interface. 功能objectAtIndex()返回一个指针CCObject ,那类绝对没有实现IRecyclableObject接口。

Thus, a brutal C-style cast of CCObject* to IRecyclableObject* will result in reinterpreting the layout of an object of the former type as if it was an object of the latter type. 因此, CCObject*IRecyclableObject*的残酷C样式IRecyclableObject*将导致重新解释前一类型的对象的布局,就好像它是后一种类型的对象一样。 That's bad, and leads to Undefined Behavior . 这很糟糕,并导致未定义的行为

You should use dynamic_cast<> to cast your CCobject* to a IRecyclableObject* : 您应该使用dynamic_cast<>CCobject*转换为IRecyclableObject*

IRecyclableObject* obj = dynamic_cast<IRecyclableObject*>(
    m_freeList->objectAtIndex(i)
    );

Notice, however, that this is not even needed if you just want your pointer to be eventually cast to an object of type CharacterAI . 但请注意,如果您只想将指针最终强制转换为CharacterAI类型的对象,则甚至不需CharacterAI Just directly cast it to that type: 只需将其直接转换为该类型:

CharacterAI* obj = dynamic_cast<CharacterAI*>(m_freeList->objectAtIndex(i));

dynamic_cast<> returns a null pointer if the run-time type of the object pointed to by the pointer you are trying to cast is not (equal to or derived from) the target type of the downcast. 如果您尝试转换的指针指向的对象的运行时类型不是(等于或派生自)目标类型的向下转换,则dynamic_cast<>返回空指针。 Therefore, in those cases where you are not sure about the concrete type of the pointed object, don't forget to check whether the returned pointer is non-null before dereferencing it. 因此,在您不确定指向对象的具体类型的情况下,请不要忘记在解除引用之前检查返回的指针是否为非null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图将基数 class 指针转换为派生 class object 实现的接口 - Trying to cast base class pointer to a interface that derived class object implements 通过实现特定接口的 Class - Passing Class which implements an specific Interface 可以使用C ++聚合初始化来构造实现接口的类的实例吗? - Can C++ aggregate initialization be used to construct an instance of a class which implements an interface? COM:使用实现接口的指针获取coclass对象的GUID - COM: Getting GUID of coclass object using pointer to interface it implements 我可以知道多态对象是否实现了任意抽象类? - Can I know if a polymorphic object implements an arbitrary abstract class? 如果没有 RTTI,在 C++ 中,我如何在运行时确定集合中的 object 是否实现接口 - Without RTTI, in C++ how can I determine at runtime if an object in a collection implements an interface 使用指向基本abstact类的指针访问子类成员,该类不能是dynamic_cast - Accessing child class members using pointer to a base abstact class, which can't be dynamic_cast 如何使用接口指针调用方法,接口指针是其具体类的一部分而不是接口的一部分 - How do I call method using interface pointer which is part of its concrete class and not part of interface 派生类的指针是否可以类型转换为其基类的指针? - Can a pointer of a derived class be type cast to the pointer of its base class? 为什么我不能将指向Derived类成员函数的指针强制转换为类Base? - why can't I cast a pointer to Derived class member function to the same but of class Base?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM