У меня есть игровая доска, полная фигурок.ostream на дочернем объекте принимает ostream своих родителей
Выделено:
board = new Figure*[boardSize];
for (int i = 0; i < boardSize; ++i) board[i] = new Figure[boardSize];
for (int i = 0; i < boardSize; ++i)
{
for (int j = 0; j < boardSize; ++j)
{
FreeSpot F(i,j);
board[i][j] = F;
}
}
У меня есть два класса
Рисунок:
class Figure {
public:
Figure ();
~Figure (void);
virtual void print (ostream & os) const;
friend ostream & operator << (ostream & os, const Figure & F);
int positionX;
int positionY;
private:
};
void Figure::print (ostream & os) const {
os << "[" << positionY << "," << positionX << "]";
}
ostream & operator << (ostream & os, const Figure & f) {
f . print (os);
return (os);
}
и, как ребенок, FreeSpot:
class FreeSpot: public Figure {
public:
FreeSpot (int, int);
~FreeSpot (void);
virtual void print (ostream & os) const;
friend ostream & operator << (ostream & os, const FreeSpot & F);
char shape;
private:
};
void FreeSpot::print (ostream & os) const {
os << "[" << positionY << shape << positionX << "]";
}
ostream & operator << (ostream & os, const FreeSpot & f) {
f . print (os);
return (os);
}
Проблема заключается в том, что если я попробуйте cout FreeSpot objec ts от доски, он берет ostream от Рис.
cout << board[i][j];
Я делаю что-то неправильно? Большое спасибо.