문제

I have a base class called 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

and I have a derived class 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

Source code for 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;
}

Now I'm trying to test the code to see if the operator overloading works properly:

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
}

As can be understood from the code, a Card holds an integer value distance and an enum value direction. It would've been a mess if i included that code too. I have commented my questions in the last piece of code test(). Thanks for your help in advance.

도움이 되었습니까?

해결책

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

What happens here is called "slicing". Since you're not storing pointers, but actual objects, the Tower part of the class is just cut off and only the Item part is pushed into the vector. To use virtual functions and polymorphism, you need a reference or pointer to the base class.

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];

Or with smart pointers from Boost or a C++11 library

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

For printing, you now obviously need to dereference the pointer:

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

}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top