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()
调用实际返回指向物理对象
的指针?请对此进行表决?如果您需要,我将添加更多解释:)