C++ 无法在对基类进行类型转换后调用方法

C++ 无法在对基类进行类型转换后调用方法,c++,inheritance,casting,box2d,cocos2d-x,C++,Inheritance,Casting,Box2d,Cocos2d X,我有Ball类,它是从PhysicsObject类派生的。在下面的方法中,我从void指针获取Ball实例,并检查其类型是否为PhysicsObject。当我调用collisdingWith方法时,它不会在Ball类对象中被调用。我做错了什么 更新 我在底部添加了一个。请参考 代码 void Levels::BeginContact(b2Contact *contact) { b2Fixture *fixtureA = contact->GetFixtureA(); b2F

我有
Ball
类,它是从
PhysicsObject
类派生的。在下面的方法中,我从
void
指针获取
Ball
实例,并检查其类型是否为
PhysicsObject
。当我调用
collisdingWith
方法时,它不会在
Ball
类对象中被调用。我做错了什么

更新 我在底部添加了一个。请参考

代码

void Levels::BeginContact(b2Contact *contact) {
    b2Fixture *fixtureA = contact->GetFixtureA();
    b2Fixture *fixtureB = contact->GetFixtureB();
    void *objA = fixtureA->GetUserData();
    void *objB = fixtureB->GetUserData();

    PhysicsObject* physicsObjA = reinterpret_cast<PhysicsObject*>(objA);
    PhysicsObject* physicsObjB = reinterpret_cast<PhysicsObject*>(objB);
    if ((physicsObjA != 0) && (physicsObjB != 0)) {
        physicsObjA->collidingWith(physicsObjB);    //not working
        physicsObjB->collidingWith(physicsObjA);
    }
}
Ball.h

#ifndef BALL_H_
#define BALL_H_

#include "Box2D/Box2d.h"
#include "cocos2d.h"
#include "PhysicsObject.h"

class Ball : public PhysicsObject {
public:
    //other methods
    void collidingWith(PhysicsObject *obj);
};

#endif /* BALL_H_ */
class AnotherBase
{
public:
    AnotherBase(void);
    ~AnotherBase(void);
    virtual void foo();
};
Ball.cpp

void Ball::collidingWith(PhysicsObject *obj) {
    CCLOG("Ball::collidingWith"); //this method is not being called
}
 //other methods

Ball::Ball() {
    //other code
    b2FixtureDef ballShapeDef;
    ballShapeDef.userData = this;
    //other code
}
更新 我还有3-4个从
PhysicsObject
派生的类,就像
Ball
,这些我在这里没有提到过,但通用代码是相同的

更新SSCCE
main.cpp
foo()

基类

class Base
{
public:
    Base(void);
    virtual ~Base(void);
    virtual void collidingWith(Base *obj) = 0;
};
另一个基类

#ifndef BALL_H_
#define BALL_H_

#include "Box2D/Box2d.h"
#include "cocos2d.h"
#include "PhysicsObject.h"

class Ball : public PhysicsObject {
public:
    //other methods
    void collidingWith(PhysicsObject *obj);
};

#endif /* BALL_H_ */
class AnotherBase
{
public:
    AnotherBase(void);
    ~AnotherBase(void);
    virtual void foo();
};
派生类标题

#include "Base.h"
#include "AnotherBase.h"
class Derived :
    public AnotherBase, Base
{
public:
    Derived(void);
    ~Derived(void);
    void collidingWith(Base *obj);
};
#include "Derived.h"

void Derived::collidingWith(Base *obj) {
    printf("Ball::collidingWith");
}
#include "Derived.h"

void myFoo(void* userData);
int _tmain(int argc, _TCHAR* argv[])
{
    Derived *derived = new Derived();
    void* userData = derived;
    myFoo(userData);
    return 0;
}

void myFoo(void* userData) 
{
    Base* baseObjA = reinterpret_cast<Base*>(userData); 
    if (baseObjA != 0) {
        baseObjA->collidingWith(baseObjA);// Error here
    }
}
派生类实现

#include "Base.h"
#include "AnotherBase.h"
class Derived :
    public AnotherBase, Base
{
public:
    Derived(void);
    ~Derived(void);
    void collidingWith(Base *obj);
};
#include "Derived.h"

void Derived::collidingWith(Base *obj) {
    printf("Ball::collidingWith");
}
#include "Derived.h"

void myFoo(void* userData);
int _tmain(int argc, _TCHAR* argv[])
{
    Derived *derived = new Derived();
    void* userData = derived;
    myFoo(userData);
    return 0;
}

void myFoo(void* userData) 
{
    Base* baseObjA = reinterpret_cast<Base*>(userData); 
    if (baseObjA != 0) {
        baseObjA->collidingWith(baseObjA);// Error here
    }
}
main.cpp

#include "Base.h"
#include "AnotherBase.h"
class Derived :
    public AnotherBase, Base
{
public:
    Derived(void);
    ~Derived(void);
    void collidingWith(Base *obj);
};
#include "Derived.h"

void Derived::collidingWith(Base *obj) {
    printf("Ball::collidingWith");
}
#include "Derived.h"

void myFoo(void* userData);
int _tmain(int argc, _TCHAR* argv[])
{
    Derived *derived = new Derived();
    void* userData = derived;
    myFoo(userData);
    return 0;
}

void myFoo(void* userData) 
{
    Base* baseObjA = reinterpret_cast<Base*>(userData); 
    if (baseObjA != 0) {
        baseObjA->collidingWith(baseObjA);// Error here
    }
}
#包括“派生的.h”
void myFoo(void*userData);
int _tmain(int argc,_TCHAR*argv[]
{
派生*派生=新派生();
void*userData=derived;
myFoo(用户数据);
返回0;
}
void myFoo(void*userData)
{
Base*baseObjA=reinterpret_cast(用户数据);
如果(baseObjA!=0){
baseObjA->与(baseObjA)冲突;//此处出错
}
}

在box2d中从void*进行铸造也给了我类似的问题

似乎是
重新解释\u cast
导致了错误,因为
冲突是一种虚拟方法。(如果该方法不是虚拟的,您应该能够毫无问题地调用它)

我已经解决了我的问题:

void* data; // You know it's type is Base

Base* base = static_cast<Base*>(data);
Derived* derived = dynamic_cast<Derived*>(base);

derived->collidingWith();
void*数据;//你知道它的类型是基本的
Base*Base=static_cast(数据);
派生*派生=动态_铸造(基础);
派生->与()冲突;

你试过投球吗?@AdriC.S。不,我没有,实际上我需要这样做,因为还有其他类,比如
Ball
,它们是从
PhysicsObject
派生的,你怎么知道它是
Ball
?球从哪里来?如果您已将
分配给
物理对象
,则可能已将其切片。能否生成一个?您是否100%确定
GetUserData()
调用实际返回指向
物理对象
的指针?请对此进行表决?如果您需要,我将添加更多解释:)