简体   繁体   English

如何在 c++ 中创建指针数组

[英]How to create an array of pointers in c++

So I have a base class called items, 2 inherited classes from items called consumable and equipment, and 2 more inherited classes from each of those.所以我有一个基础 class 称为项目,从称为消耗品和设备的项目继承的 2 个类,以及从每个继承的 2 个类。 Each have the function toString(), with it building up as you go down the inherited list.每个都有 function toString(),随着你 go 在继承列表中的建立。 But here is my issu.但这是我的问题。 I need to put 4 objects inherited from consumable and equipment into an array of items by pointers.我需要通过指针将从消耗品和设备继承的 4 个对象放入一个项目数组中。 How do I do that?我怎么做?

I have this code currently in my main:我的主要代码中目前有此代码:

Weapon greatSword("Great Sword", "A sword forged for mighty warriors", 12.50, true, 1, 150.00f, false, 13, "Slashing");
Armor mekiChestplate("Meki's Chest Plate", "A chest plate given to me by my mother", 1500.25f, false, 25, 2000.50f, true, "Chest", 18);
Food pie("Grandma's Pie", "Baked with love and affection", 10.35f, true, 5, 150, 0);
Potion spiderVenom("Spider's Venom", "Poision picked up from a spider", 100.55f, false, 10, 100, 1);

Item inventory[4]
{
    greatSword, 
    mekiChestplate,
    pie,
    spiderVenom
};

for(int i = 0; i < 4; i++)
{
    cout << inventory[i].toString() << endl;
    cout << "-----------------------------------------------------------" << endl;
}

return 0;

This works minus that they don't call their own toString, just the Item toString.这有效减去他们不调用自己的toString,只是Item toString。 How can I put the 4 items into an array of pointers?如何将 4 个项目放入指针数组中?

As mentioned in comments above;如上面评论中所述; the array should be an Item* type.数组应该是 Item* 类型。 Since question is posted as C++14, using <array> allows you to access the values by reference;由于问题发布为 C++14,使用 <array> 允许您通过引用访问值; instead of manually indexing.而不是手动索引。 This is more scalable as the for loop doesn't have to be updated in the future.这更具可扩展性,因为将来不必更新 for 循环。 It also eliminates possible bugs or errors when the array size doesn't match the 'for' loop.当数组大小与“for”循环不匹配时,它还消除了可能的错误或错误。

In order to access the child class function using the base class pointer, a virtual function needs to be defined.为了使用基本 class 指针访问子 class function,需要定义虚拟 ZC1C425268E68395D1AB504 The example below demonstrates how to implement the virtual function in each class.下面的示例演示如何在每个 class 中实现虚拟 function。

For bonus points, it also demonstrates how to call the parent's toString within each child class.对于奖励积分,它还演示了如何在每个子 class 中调用父级的 toString。

#include <array>
#include <string>
#include <iostream>

class Item
{
public:
    Item(const char* const name):
        mName(name)
    {}
    
    virtual std::string toString() const
    {
        return mName;
    }
private:
    std::string mName;    
    
};

class Equipment : public Item
{
public:
    Equipment(const char* const name):
        Item(name)
    {}
    
    virtual std::string toString() const override
    {
        const std::string extraInfo {"Equipment->"};
        const std::string item {Item::toString()};
        return extraInfo + item;
    }
};

class Consumable : public Item
{
public:
    Consumable(const char* const name):
        Item(name)
    {}
    
    virtual std::string toString() const override
    {
        const std::string extraInfo {"Consumable->"};
        const std::string item {Item::toString()};
        return extraInfo + item;
    }
};

class Weapon : public Equipment
{
public:  
    Weapon(const char* const name):
        Equipment(name)
    {}

    virtual std::string toString() const override
    {
        const std::string extraInfo {"Weapon->"};
        const std::string item {Equipment::toString()};
        return extraInfo + item;
    }
};

int main()
{
    Weapon greatSword("Great Sword");
    Weapon mekiChestplate("Meki's Chest Plate");
    Consumable pie("Grandma's Pie");
    Consumable spiderVenom("Spider's Venom");
    
    std::array<Item*, 4> inventory
    {
        &greatSword, 
        &mekiChestplate,
        &pie,
        &spiderVenom
    };
    
    for (auto it : inventory)
    {
        std::cout << it->toString() << std::endl;
        std::cout << "-----------------------------------------------------------" << std::endl;        
    }
    
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM