ostream运算符重载-继承
ostream operator overloading - inheritance
我有一个名为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
}
从代码中可以理解,卡包含一个整数值距离和一个枚举值方向。如果我也包含那个代码的话,那就太糟糕了。我在最后一段代码test((中评论了我的问题。感谢您提前提供的帮助。
std::vector<Item> items; //creating an Item vector
items.push_back(Tower(one));
这里发生的事情叫做"切片"。由于不是存储指针,而是存储实际对象,所以类的Tower
部分被截断,只有Item
部分被推入向量中。要使用虚拟函数和多态性,需要一个指向基类的引用或指针。
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++中的函数重载和继承
- C++ 继承:基类中重载 operator+ 的 2 次在派生类中无法正常工作
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- 类继承和运算符重载
- 通过继承重载运算符会导致歧义
- 使用继承的指针列表复制构造函数或重载运算符=
- 重载<<无法访问继承的私有数据
- C++:涉及继承和"using"时的方法重载
- C++具有多重继承的构造函数重载解析
- c++ 中的函数重载如何在没有钻石继承的情况下工作?
- 函数不参与继承中的重载解析
- 继承类 C++ 中的重载>>
- 模板和unique_ptr继承情况下的重载解决方案
- 如何将同名的继承函数视为重载函数
- 继承重载函数:需要命名空间
- 派生类没有从基类继承重载方法
- c++继承+重载
- c++继承重载成员