这个新类的东西,很可能做错了什么,而是 -C++交换的unique_ptr的
我有3名成员
std::unique_ptr<Gun> currentWeapon;
std::unique_ptr<Gun> weaponSlotOne;
std::unique_ptr<Gun> weaponSlotTwo;
枪是具有其他派生类如Pistol
和SMG
一个基类。
我在做的是将weaponSlotOne
和weaponSlotTwo
设置为两种不同的枪,然后将currentWeapon
设置为第一个武器。
weaponSlotOne.reset(new DevPistol());
weaponSlotTwo.reset(new AutoDevPistol());
currentWeapon = std::move(weaponSlotOne);
和我有一个switchWeapons
方法,做这个:
void Player::switchWeapons() {
if(currentWeapon == weaponSlotOne) {
currentWeapon = std::move(weaponSlotTwo);
}
else {
currentWeapon = std::move(weaponSlotOne);
}
}
这似乎破坏/解除分配双枪出于某种原因。我不太确定发生了什么问题。
'unique_ptr'在这种情况下没有意义 – 2014-11-25 01:53:04
也许:http://en.cppreference.com/w/cpp/memory/unique_ptr/release? – Galik 2014-11-25 01:53:05