這個新類的東西,很可能做錯了什麼,而是 -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