【发布时间】:2016-03-10 03:47:32
【问题描述】:
我正在尝试用 C++ 构建我的第一个游戏,并且我正在尝试制作一个 Inventory 系统,该系统将允许我在 Inventory 容器类型中管理和安排不同的 Item 数据类型。在下面的代码中,我只编写了类声明,但我想知道如何使我创建的“Item”类包含任何不同继承项的数据类型,例如Weapon、Apparel和Aid 类别。会像使用template 参数一样简单吗?如果是这样,那么我的问题是Constructor 之类的东西如何理解在创建项目的每个实例中要做什么? default Constructor 可能必须是我决定设置的基本武器,对吗?如果有人有任何建议或链接提供,将不胜感激。
谢谢!
项目.H:
#ifndef ITEM_H
#define ITEM_H
#include <iostream>
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
class Item
{
public:
private:
};
/*************************************************
* WEAPON CLASS
* *This class is a type of Item that classifies a
* weapon item that can be in an Inventory.
**************************************************/
class Weapon : public Item
{
public:
private:
};
/*************************************************
* APPAREL CLASS
* *This class is a type of Item that classifies an
* apparel item that can be in an Inventory.
**************************************************/
class Apparel : public Item
{
public:
private:
};
/*************************************************
* AID CLASS
* *This class is a type of Item that classifies an
* Aid item that can be in an Inventory.
**************************************************/
class Aid : public Item
{
public:
private:
};
#endif //ITEM_H
编辑:
这是我现在所拥有的:
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
template <class T>
class Item
{
public:
private:
T item_type; //i.e. Weapon, Apparel, Aid, etc...
};
/*****************************************************************
* ITEM : WEAPON
* *Below is the code for all the different Weapon classes.
******************************************************************/
/*************************************************
* WEAPON CLASS
* *This class is a type of Item that classifies a
* weapon item that can be in an Inventory.
**************************************************/
template <class T>
class Weapon : public Item
{
public:
private:
T weapon_type; //i.e. Melee, Ranged, etc...
};
/*************************************************
* MELEE CLASS
* *This class is a type of Item that classifies a
* melee item that will be used as a weapon.
**************************************************/
template <class T>
class Melee : public Weapon
{
public:
private:
};
/*************************************************
* RANGED CLASS
* *This class is a type of Item that classifies a
* ranged item that will be used as a weapon.
**************************************************/
template <class T>
class Ranged : public Weapon
{
public:
private:
};
另一个编辑: 这是最新的代码:
/*************************************************
* ITEM CLASS
* *This class is the type of data to be put into
* the Inventory Container class.
**************************************************/
template <class T>
class Item
{
public:
protected:
T item_type; //i.e. Weapon, Apparel, Aid, etc...
};
/*****************************************************************
* ITEM : WEAPON
* *Below is the code for all the different Weapon classes.
******************************************************************/
//WEAPON CLASS
template <class T>
class Weapon : public Item
{
public:
protected:
T weapon_type; //i.e. Melee, Ranged, etc...
};
//MELEE CLASS
template <class T>
class Melee : public Weapon
{
public:
protected:
T melee_type; //i.e. Blade, Blunt, Fist, etc...
};
//BLADE CLASS
template <class T>
class Blade : public Melee
{
public:
protected:
T blade_type; //i.e. Dagger, Sword, etc...
};
//DAGGER CLASS
template <class T>
class Dagger : public Blade
{
public:
//Default Constructor...the only Constructor
Dagger() : damage(1), weight(0.5) { }
//getters
std::string getType() const { return "Dagger"; }
protected:
int damage; //amount of damage it can inflict
float weight; //weight of the dagger
};
【问题讨论】:
-
我不是在谈论 Inventory 类。我的意思是 Item 类中的成员变量。
-
你想做什么?
-
我想弄清楚如何在
Item中设置成员变量,以便它可以是任何类别,如武器、服装等,但我不确定模板是否是最适合它。但我也不知道有什么其他方法可以处理类似的事情。 -
@songyuanyao 是的。这就是我的意思。
-
我在继承列表中放置的模板是否应该有不同的字母?所以像物品 -> 武器 -> 近战 -> 剑......他们每个人应该有不同的模板参数吗?