c++ - ostream 运算符重载 - 继承

标签 c++ inheritance operator-overloading ostream

我有一个名为 Item 的基类:

#ifndef ITEM_H
#define ITEM_H

#include <ostream>

class Item {
public:
    virtual ~Item() {}
    virtual void print(std::ostream& out) const {}
    friend std::ostream& operator << (std::ostream& out, Item& item){
    item.print(out);
    return out;
    }
};



#endif

我有一个派生类 Tower:

#ifndef TOWER_H
#define TOWER_H

#include <iostream>
#include <iostream>
#include <exception>
#include "item.h"
#include "Card.h"

class Tower : public Item {
    unsigned height;
        void print(std::ostream& o) const;
public:
    Tower(const Card& card);
    int demolish(Card& card) const;
    unsigned getHeight() const;
};

#endif

Tower 的源代码:

#include "tower.h"



Tower::Tower(const Card& card){
    height = card.getDis();
}

void Tower::print(std::ostream& o) const {
    o << height;
}


int Tower::demolish(Card& card) const{
    try {
    if(height != card.getDis()){
            throw std::exception ();
        } else {
            return height;
        }
    } catch (std::exception e){
        cout<< "Card's value not enough to demolish the tower." << endl;
    }
}

unsigned Tower::getHeight() const {
    return height;
}

现在我正在尝试测试代码以查看运算符重载是否正常工作:

void test() {
    Card card (Card::SOUTH, 3);
    Tower tower(card);

    std::cout << "Printing tower: " << tower << std::endl;  //PRINTS OUT 3 AS EXPECTED

    Card one (Card::NORTH, 2);
    Card two (Card::WEST, 3);

    std::cout << "Expecting to receive an error: " <<endl;
    tower.demolish(one);

    std::cout << "Expecting to have the tower demolished: ";
    std::cout << tower.demolish(two) <<std::endl;

    std::cout << "Height of the tower: " << tower.getHeight() <<std::endl;

    std::vector<Item> items;       //creating an Item vector
    items.push_back(Tower(one));

    Item items2[1];                //creating an array of Items
    items[0]= tower;

    std::cout << "Printing out an Item: ";    
    std::cout << items.back()<<std::endl;   //EXPECTING TO GET 2 BUT IT PRINTS NOTHING, WHY IS THAT?
    std::cout << "OR: " << items2[0]<<std::endl;  //SAME ISSUE HERE, EXPECTING TO GET 3
}

从代码中可以看出,Card 包含一个整数值distance 和一个枚举值direction。如果我也包含该代码,那将是一团糟。我在最后一段代码 test() 中评论了我的问题。提前感谢您的帮助。

最佳答案

std::vector<Item> items;       //creating an Item vector
items.push_back(Tower(one));

这里发生的事情叫做“切片”。由于您存储的不是指针,而是实际对象,因此类的 Tower 部分被截断,只有 Item 部分被插入 vector 。要使用虚函数和多态性,您需要一个指向基类的引用或指针。

std::vector<Item*> items;       //creating an Item vector
items.push_back(new Tower(one));
// ...
// at the end of main:
for(int i=0; i < items.size(); ++i)
  delete items[i];

或者使用来自 Boost 或 C++11 库的智能指针

std::vector<shared_ptr<Item>> items;
items.push_back(make_shared<Tower>(one));
// nothing further needs to be done

对于打印,您现在显然需要取消引用指针:

std::cout << "Printing out an Item: ";    
std::cout << *items.back()<<std::endl;
std::cout << "OR: " << *items2[0]<<std::endl;

关于c++ - ostream 运算符重载 - 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373725/

相关文章:

c++ - 使用模板时,是否可以确定左值的类型?

c++ - clang 和 gcc 在使用智能指针时不会警告多态性的非虚基析构函数?

c++ - 任何基于 libclang 的 C/C++ 重构工具? (甚至最简单的 "toy example")

Java数组继承困惑

c++ - 重载 operator<< 时 std::endl 的类型未知

c++ - 在 C++ 中获取内存地址而不使用与号 (&) 运算符

c++ - 类模板化 std::set 键类型

C++具体继承示例

C++ Virtual boost::any 继承

c++ - 后缀运算符重载顺序