W
woosu
Hello ladies and gentlemen.
I have a relatively simple problem that I've been unable to solve.
In the interest of learning C++, I've decided to write a simple game.
The basis for the game is 8x8 board (think chess). I decided to
implement iterators so it'd be easy to analyze the board position etc.
Basically I have the following hierachy:
board_iterator - generic iterator that walks the board left to right as
| if it was a vector (which it is)
|_ horizontal_iterator - an iterator that walks rows only
|_ veftical_iterator - an iterator that walks columns only
|_ diagonal_iterator - an iterator that can walk diagonals (either
left to right or right to left).
So here's the problem. I want to overload (for example) operator+(). As
far as I understand, operator+() returns a new instance of an object.
So, to put this in code:
class board_iterator
{
...
board_iterator operator+(int foo)
{
board_iterator new = *this; // <--- problem.
new.plus(foo);
return new;
}
virtual void plus(...)
{
// do some work that's board_iterator specific
}
...
};
class board_iterator_vertical: public board_operator
{
virtual void plus(...)
{
// do some work that's board_iterator_vertical specific
}
};
So lets say I want to do something like this:
board_iterator i,j,k;
i = j + 3;
This works fine.
But another scenario will not:
board_iterator i,j,k;
board_iterator_vertical a,b,c;
a = i + 3;
This will not work. board_iterator_vertical inherits operator+()
from board_iterator which returns board_iterator on operator+() instead
of board_iterator_vertical. How do I get around this? The key here is
that all operator+() will do the same:
1. create a new object (of proper type!! this is the key!)
2. run virtual plus() on it (which will be overloaded for all of
the
types).
3. Return it.
So how do I fix this? Obvious solution is to have operator+() for every
inherited class, instead of having operator+() call plus() but that
seems wrong somehow. In particular, the only difference between all of
the operator+() methods will be the way that next location is
calculated. It's not crucial in the long run, but I want to understand
what I'm doing wrong. I also want to do this almost-right.
Thoughts and opinions?
Thanks.
I have a relatively simple problem that I've been unable to solve.
In the interest of learning C++, I've decided to write a simple game.
The basis for the game is 8x8 board (think chess). I decided to
implement iterators so it'd be easy to analyze the board position etc.
Basically I have the following hierachy:
board_iterator - generic iterator that walks the board left to right as
| if it was a vector (which it is)
|_ horizontal_iterator - an iterator that walks rows only
|_ veftical_iterator - an iterator that walks columns only
|_ diagonal_iterator - an iterator that can walk diagonals (either
left to right or right to left).
So here's the problem. I want to overload (for example) operator+(). As
far as I understand, operator+() returns a new instance of an object.
So, to put this in code:
class board_iterator
{
...
board_iterator operator+(int foo)
{
board_iterator new = *this; // <--- problem.
new.plus(foo);
return new;
}
virtual void plus(...)
{
// do some work that's board_iterator specific
}
...
};
class board_iterator_vertical: public board_operator
{
virtual void plus(...)
{
// do some work that's board_iterator_vertical specific
}
};
So lets say I want to do something like this:
board_iterator i,j,k;
i = j + 3;
This works fine.
But another scenario will not:
board_iterator i,j,k;
board_iterator_vertical a,b,c;
a = i + 3;
This will not work. board_iterator_vertical inherits operator+()
from board_iterator which returns board_iterator on operator+() instead
of board_iterator_vertical. How do I get around this? The key here is
that all operator+() will do the same:
1. create a new object (of proper type!! this is the key!)
2. run virtual plus() on it (which will be overloaded for all of
the
types).
3. Return it.
So how do I fix this? Obvious solution is to have operator+() for every
inherited class, instead of having operator+() call plus() but that
seems wrong somehow. In particular, the only difference between all of
the operator+() methods will be the way that next location is
calculated. It's not crucial in the long run, but I want to understand
what I'm doing wrong. I also want to do this almost-right.
Thoughts and opinions?
Thanks.