如何从继承的自定义类 c++ 创建类型

How to make a type out of inherited custom classes c++

本文关键字:c++ 创建 类型 自定义 继承      更新时间:2023-10-16

我正在尝试用 c++ 构建我的第一个游戏,并且我正在尝试制作一个 Inventory 系统,该系统允许我在Inventory容器类型中管理和排列不同的Item数据类型。在下面的代码中,我只组成了类声明,但我想知道如何使我创建的"Item"类保存任何不同继承项(如WeaponApparelAid类别)的数据类型。它会像使用 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
};

一种解决方案是将类型定义为 enum s。

enum ITEM_TYPE { IT_WEAPON, IT_END };
enum WEAPON_TYPE { WT_MELEE, WT_RANGED, WT_END };
class Item {
public:
    Item(ITEM_TYPE type) : item_type(type) {}
protected:
    ITEM_TYPE item_type;
};
class Weapon : public Item {
public:
    Weapon(WEAPON_TYPE type) : Item(IT_WEAPON), weapon_type(type) {}
protected:
    WEAPON_TYPE weapon_type;
};
class MeleeWeapon : public Weapon {
public:
    MeleeWeapon() : Weapon(WT_MELEE) {}
};
class RangedWeapon : public Weapon {
public:
    RangedWeapon() : Weapon(WT_RANGED) {}
};

你不能也不应该需要存储类型(C++语言意义上的类型),使用虚函数来实现你想要的。

#include <iostream>
#include <memory>
#include <vector>
class Item
{
public:
     virtual const char* type() const = 0;
};
class Weapon : public Item
{
public:
      const char* type() const override { return "Weapon"; }
};
class Ranged : public Weapon
{
public:
    const char* type() const override { return "Ranged"; }
};
class Melee : public Weapon
{
public:
    const char* type() const override { return "Melee"; }
};
class Apparel : public Item
{
public:
    const char* type() const override { return "Apparel"; }
};
int main()
{
    std::vector<std::unique_ptr<Item>> items;
    items.emplace_back(new Apparel());
    items.emplace_back(new Ranged());
    items.emplace_back(new Melee());
    std::cout << "Inventory Types:n";
    for (const auto& it: items)
        std::cout << it->type() << 'n';
}

见 http://cpp.sh/6cjlt