gpt4 book ai didi

iphone - 为什么这个C++(cocos2d,box2d)例子给出的是指针而不是值?

转载 作者:行者123 更新时间:2023-11-30 02:05:45 25 4
gpt4 key购买 nike

我创建了这个结构:

struct MyBodyData
{
int someNumber;
};

然后创建了一个新的body:

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(0.0f, -10.0f);
b2Body* body1 = world->CreateBody(&bodyDef);
MyBodyData *bodyData = new MyBodyData();
bodyData->someNumber = 4;
body1->SetUserData(&bodyData);

b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body1->CreateFixture(&fixtureDef);

然后尝试在此处访问 someNumber:

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
CCLOG(@"Hello One");
MyBodyData* data = (MyBodyData*)b->GetUserData();
if (data!=0 && data->someNumber != 0)
{
int temp;
temp = data->someNumber;
CCLOG(@"Hello Again! %d",temp);
}
}

它将 someNumber 显示为 6-7 位长的内存地址,而不是值“4”。

我错过了什么?

最佳答案

body1->SetUserData(&bodyData);

您正在将正文数据设置为指针的地址 bodyData。你不想这样,你想将它设置为 bodyData。所以:

body1->SetUserData(bodyData);

关于iphone - 为什么这个C++(cocos2d,box2d)例子给出的是指针而不是值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9402640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com