samedi 6 octobre 2018

Overload operator* to get pointer to instance of another class

I have classes

class Square
{
public:
    virtual void apply(Player*) = 0; //apply square effect to the player
};

class BoardIterator
{
public:
    BoardIterator();
    Square &operator*();//return current square where player is
    Square &operator+(int);//move forward certain number of squares
private:
    static const int pathLength = 8;//length of outer path
    static const int innerPathLength = 4;//length of inner path
    int curPosition;//number of current square
    Square *outerPath[pathLength];
    Square *innerPath[innerPathLength];
};

*boardIterator has to return pointer to Square object(also I have child classes for Square). This is my implementation of this operator

Board::Square& Board::BoardIterator::operator*()
{
     return *(outerPath[curPosition]);
}

But cant get pointer to Square object if I write *(*boardIterator)(where boardIterator is pointer to BoardIterator class). How should I implement this overlading in right way?

Aucun commentaire:

Enregistrer un commentaire