C++ 将向量元素与指针从一个派生类交换到另一个派生类
我有一个国际象棋游戏,最初使用stl列表来存储棋子,为了更好的性能,我将其转换为向量。我知道向量不支持多态性,所以为了解决这个问题,我存储了C++ 将向量元素与指针从一个派生类交换到另一个派生类,c++,pointers,stl,vector,polymorphism,C++,Pointers,Stl,Vector,Polymorphism,我有一个国际象棋游戏,最初使用stl列表来存储棋子,为了更好的性能,我将其转换为向量。我知道向量不支持多态性,所以为了解决这个问题,我存储了的向量,而不是。我的所有棋子对象(兵、车、主教等)都从Unit类继承 然而,向量和堆损坏似乎仍然存在问题。我想我已经找到了以下功能: Unit *ChessGame::PromoteUnit(Unit *_oldUnit, UnitType _newType) { vector<Unit *> &army = (_oldUnit->
的向量,而不是
。我的所有棋子对象(兵、车、主教等)都从Unit类继承
然而,向量和堆损坏似乎仍然存在问题。我想我已经找到了以下功能:
Unit *ChessGame::PromoteUnit(Unit *_oldUnit, UnitType _newType)
{
vector<Unit *> &army = (_oldUnit->m_gameColor == WHITE) ? m_whiteArmy : m_blackArmy;
Unit *newUnit = NULL;
for (unsigned int i = 0; i < army.size(); ++i)
{
if (army[i]->m_subId == _oldUnit->m_subId)
{
if (_newType == QUEEN && _oldUnit->m_gameColor == WHITE)
{
newUnit = new Queen(*_oldUnit);
newUnit->ActiveTexture_(m_textureMan->TextureId_(WhiteQueen));
}
else if (_newType == KNIGHT && _oldUnit->m_gameColor == WHITE)
{
newUnit = new Knight(*_oldUnit);
newUnit->ActiveTexture_(m_textureMan->TextureId_(WhiteKnight));
}
else if (_newType == QUEEN && _oldUnit->m_gameColor == BLACK)
{
newUnit = new Queen(*_oldUnit);
newUnit->ActiveTexture_(m_textureMan->TextureId_(BlackQueen));
}
else if (_newType == KNIGHT && _oldUnit->m_gameColor == BLACK)
{
newUnit = new Knight(*_oldUnit);
newUnit->ActiveTexture_(m_textureMan->TextureId_(BlackKnight));
}
newUnit->m_wasPawn = true;
delete army[i];
army[i] = newUnit;
break;
}
}
m_selectedUnit = newUnit;
return newUnit;
}
当它试图使用malloc保留堆内存时,进入new Pawn()会导致它在新操作符中崩溃。无论如何,我认为这仍然与我对向量stl如何工作缺乏全面的理解有关。我知道它与我的Pawn()构造函数无关,因为它在游戏板初始化期间被多次调用。我怀疑,但无法从您共享的信息证明,您的复制构造函数和复制赋值运算符用于
ChessGame
对m_whiteArmy
和m_blackArmy
进行浅层复制。(注意:如果您不提供复制构造函数或复制赋值运算符,编译器将为您提供它们。编译器提供的是浅层复制。)
你违反了法律
您可以通过以下方式解决此问题:
- 从不复制
对象棋盘游戏
- 通过以下方式强制执行上述规定:
- (C++11之前):声明和不定义复制构造函数和复制赋值运算符
- (C++11):在复制构造函数和复制赋值运算符声明之后指定
=delete
- 将指针更改为智能指针(如
),或std::shared_ptr
- 在复制构造函数和复制赋值运算符中实现深度复制
new
和delete
的使用情况。看看你是否能证明这些断言:对于每一个new
来说,只有一个delete
;对于每个new[]
,只有一个delete[]
。如果您的代码路径太复杂,无法证明该断言,请尝试简化代码,或者使用std::shared_ptr
而不是原始指针。
Unit *ChessGame::DemoteUnit(Unit *_oldUnit, UnitType _newType)
{
COUT("ChessGameManager::_DemoteUnit(Unit *, UnitType)");
vector<Unit *> &army = (_oldUnit->m_gameColor == WHITE) ? m_whiteArmy : m_blackArmy;
Unit *newUnit = NULL;
for (unsigned int i = 0; i < army.size(); ++i)
{
if (army[i]->m_subId == _oldUnit->m_subId)
{
newUnit = new Pawn();
newUnit->m_wasPawn = false;
if (_oldUnit->m_gameColor == WHITE)
newUnit->ActiveTexture_(m_textureMan->TextureId_(WhitePawn));
newUnit->m_gameColor = _oldUnit->m_gameColor;
newUnit->MobilityValid_(false);
newUnit->Color_(RvColor::ClrWhite);
newUnit->m_square = _oldUnit->m_square;
newUnit->m_captured = false;
newUnit->m_origin = _oldUnit->m_origin;
newUnit->m_subId = _oldUnit->m_subId;
newUnit->m_visible = true;
//newUnit->m_square->m_unit = newUnit;
delete army[i];
army[i] = newUnit;
break;
}
}
return newUnit;
}
newUnit = new Pawn();