C++ C++;:如何建立一个;项目「;正确分类
我目前正在制作一个2D平台游戏引擎,但是我似乎无法为我的“项目”类创建一个优雅的设计 我所说的物品是什么意思:玩家可以在他/她的物品清单中持有的任何东西,例如铁或治疗药剂 如何融入我的项目-3个类处理“项目”:C++ C++;:如何建立一个;项目「;正确分类,c++,game-engine,C++,Game Engine,我目前正在制作一个2D平台游戏引擎,但是我似乎无法为我的“项目”类创建一个优雅的设计 我所说的物品是什么意思:玩家可以在他/她的物品清单中持有的任何东西,例如铁或治疗药剂 如何融入我的项目-3个类处理“项目”: “库存”类 “级别”类(存储丢弃的“项”的位置) “玩家\设备\插槽”类。这就是问题的表现: #include "Globals.h" #include "Item.h" class Player_Equipment_Slots { public: Player_Equipment_S
#include "Globals.h"
#include "Item.h"
class Player_Equipment_Slots
{
public:
Player_Equipment_Slots();
Player_Equipment_Slots(Player& playerObj);
virtual ~Player_Equipment_Slots();
// return: the item that was already equipped, if there was one already equipped
Item equipItem(Item itemToEquip);
Attributes getTotalAttributes() const { return totalAttributes; }
private:
// applies passive effects equipment may have to the player and removes any existing one (called by equipItem)
void updateEquipmentEffects(Item& newEquipment, Item& oldEquipment);
// subtracts the stats of the old item and adds the new items stats (called by equipItem)
void updateTotalAttributes();
Item necklaceSlot;
Item ringSlot1;
Item ringSlot2;
Item trinket1;
Item trinket2;
Attributes totalAttributes;
Player& playerObj;
};
ItemDetails();
ItemDetails(std::string itemName, itemType itmType, rarity itemRarity, Coordinate itemIconCoord, std::string effect);
理想情况下,我希望去掉默认构造函数,因为只应使用第二个构造函数创建项,但是如果我这样做,Player\u Equipment\u Slots类将在编译时抱怨,这是可以理解的,因为-
Item necklaceSlot;
Item ringSlot1;
Item ringSlot2;
Item trinket1;
Item trinket2;
对象需要调用默认构造函数
问题:我将如何重新设计这些类,使我的物品类没有默认构造函数,但其他类(如玩家\设备\插槽)可以存储私人物品对象。
我知道这可以通过指针轻松实现,但我希望避免每次创建新项目时使用“new”关键字。也许还有一种方法我可以使用指针而不使用“new”关键字,但是我不知道如何实现它。使用继承,该项用于不同的使用类别(只是一个建议),如果您不想使用默认构造函数,只需删除它
Item()=删除
或放入类的私有部分