gpt4 book ai didi

c++ - 为什么这个对象会消失并被删除?

转载 作者:行者123 更新时间:2023-11-28 01:11:49 24 4
gpt4 key购买 nike

通过调试器,BBox对象在函数入口是没问题的,但是一进入函数,vfptr对象就指向0xccccc。我不明白。

  1. 这是什么原因造成的?
  2. 当对象不是从其他类派生时,为什么那里有一个虚拟表引用。 (虽然,它驻留在我的 Player 类继承的 GameObject 中,我从播放器中检索 BBox。但是,为什么 BBox 有引用?不应该是应该在该引用中维护的播放器吗?)

对于 1;一些代码供引用:

一个。我从播放器中检索边界框。这将按预期返回边界框。然后我将它的地址发送给 GetGridCells。

 const BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();

boost::unordered_set < Cell*, CellPHash >& l_GridCells = GetGridCells ( &l_Bbox );

B.这就是 a_​​pBoundingBox 变得疯狂并获得垃圾值的地方。

 boost::unordered_set< Cell*, CellPHash > CollisionMgr::GetGridCells(const BoundingBox *a_pBoundingBox)
{

我认为下面的代码也是相关的,所以我还是把它贴在这里:

 const BoundingBox& Player::GetBoundingBox(void)
{
return BoundingBox( &GetBoundingSphere() );
}

const BoundingSphere& Player::GetBoundingSphere(void)
{
BoundingSphere& l_BSphere = m_pGeomMesh->m_BoundingSphere;

l_BSphere.m_Center = GetPosition();

return l_BSphere;
}

// BoundingBox Constructor
BoundingBox(const BoundingSphere* a_pBoundingSphere);

谁能告诉我为什么会这样?另外,如果您希望我发布更多代码,请告诉我。

谢谢!

最佳答案

 const BoundingBox& Player::GetBoundingBox(void)
{
return BoundingBox( &GetBoundingSphere() );
}

在这里,您将返回对临时 BoundingBox 对象的引用。 return 语句一结束,该对象就会超出范围。

返回 BoundingBox 而不是 BoundingBox&


还有:

 BoundingSphere& l_BSphere = m_pGeomMesh->m_BoundingSphere;

l_BSphere.m_Center = GetPosition();

在这里,您引用了 m_pGeomMesh 的边界球体,然后修改它引用的值。这将导致对原始对象的修改。你确定这是你想要的吗?


还有:

 // BoundingBox Constructor
BoundingBox(const BoundingSphere* a_pBoundingSphere);

在唯一一个使用引用很有意义的地方,你使用了一个指针。为什么?

关于c++ - 为什么这个对象会消失并被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2612709/

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