Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载基类和派生类中的运算符_C++ - Fatal编程技术网

C++ 重载基类和派生类中的运算符

C++ 重载基类和派生类中的运算符,c++,C++,我有一个基类,物品和派生类,武器和盾牌。这两个重载std::vector都将其元素保留在自己的私有数组中,因此它们按值存储。Item类型的值始终将Item作为其最派生的类,因此当您在其中存储武器时,保存在向量中的仅是武器的Item部分,并且在打印时,它只能将自己报告为Item。std::vector将其元素保留在其自己的私有数组中,因此它们按值存储。Item类型的值始终将Item作为其最派生的类,因此,当您在其中存储武器时,向量中保存的只是一个Item,即武器的Item部分,并且在打印时,它只能

我有一个基类,物品和派生类,武器和盾牌。这两个重载std::vector都将其元素保留在自己的私有数组中,因此它们按值存储。Item类型的值始终将Item作为其最派生的类,因此当您在其中存储武器时,保存在向量中的仅是武器的Item部分,并且在打印时,它只能将自己报告为Item。

std::vector将其元素保留在其自己的私有数组中,因此它们按值存储。Item类型的值始终将Item作为其最派生的类,因此,当您在其中存储武器时,向量中保存的只是一个Item,即武器的Item部分,并且在打印时,它只能将自身报告为Item。

除此之外,您的代码还存在另一个概念问题。您希望运算符除了,您的代码还存在另一个概念问题。您对运算符的期望当您使用vector inventory时,您的代码会受到影响。您应该真正瞄准解决您试图解决的问题而不是其他问题的运算符。当您使用vector inventory时,您的代码会受到影响。您应该真正瞄准解决您试图解决的问题而不是其他问题的运算符。因此,这样设置可以让我保留向量并调用武器的指纹?@MyCodeSucks,不,这是一个正交问题,也必须解决。使用智能指针的std::vector。因此,这样设置将允许我仍然拥有该向量并调用武器的打印?@MyCodeSucks,不,这是一个正交问题,也必须解决。使用智能指针的std::vector。
// item(base) operator overload
ostream& operator<<(ostream& os, const Item& a_item)
{
    os << "     Item Object - " << endl;
    os << "          Name: " << a_item.m_name << endl;
    os << "          Cost: " << a_item.m_cost << endl;
    os << "          Purpose: " << a_item.m_purpose << endl;

    return os;
}
// weapon(derived) operator overload
ostream & operator<<(ostream & os, const Weapon & a_weapon)
{
    os << "Weapon - " << endl;
    os << "     Name: " << a_weapon.m_name << endl;
    os << "     Cost: " << a_weapon.m_cost << endl;
    os << "     Purpose: " << a_weapon.m_purpose << endl;

    return os;
}

// shield(derived) operator overload
ostream & operator<<(ostream & os, const Shield & a_shield)
{
    os << "Shield - " << endl;
    os << "     Name: " << a_shield.m_name << endl;
    os << "     Cost: " << a_shield.m_cost << endl;
    os << "     Purpose: " << a_shield.m_purpose << endl;

    return os;
}
// person(derived) operator overload
ostream& operator<<(ostream& os, const Person& a_person)
{
    os << "Person Object - " << endl;
    os << "     Name: " << a_person.m_name << endl;
    os << "     Level: " << a_person.m_level << endl;
    os << "     Hit Points: " << a_person.m_hit_points << endl;
    os << "     Inventory: " << endl;

    for (auto i = 0; i < a_person.m_inventory.size(); i++)
    {
        os << "     " << a_person.m_inventory[i] << endl;
    }

    return os;
}
struct Base
{
   virtual std::ostream& print(std::ostream& os) const
   {
      // Print base class details

      return os;
   }
};

struct Derived : Base
{
   virtual std::ostream& print(std::ostream& os) const
   {
      // Do the base class printing first
      Base::print(os);

      // Print the derived class details.

      return os;
   }
};

std::ostream& operator<<(std::ostream& os, Base const& b)
{
   // This will be a polymorphic call.
   return b.print(os);
}