Search code examples
c++functionoopfriend

How can I call function of another class?


class Bishop : public ChessPiece {
    public:
    friend class Queen;
    Bishop(string colorIn, string nameIn);

    //isLegalMove for bishop
    //implemented this function for queen
    bool isLegalBishopMove(int xSrc, int ySrc, int xDest, int yDest);

    //Can move along any diagnol
    virtual bool isLegalMove(int xSrc, int ySrc, int xDest, int yDest) ;
};


class Queen : public ChessPiece {
 public:
    //friend class Bishop;

    Queen(string colorIn, string nameIn);
    //friend bool Bishop::isLegalBishopMove(int xSrc, int ySrc, int xDest, int yDest);

    virtual bool isLegalMove(int xSrc, int ySrc, int xDest, int yDest);
};

I want my Queen's class implementation of isLegalMove to be able to call the function isLegalBishopMove. How can I solve this? I tried using friend but it did not work. I am not understanding the c++ references.


Solution

  • I would think there is nothing wrong in free functions or public static methods. I prefer them to inheritance.

    static bool Queen::isLegalMove(Position poscurr, Position posnew)
    
    static bool Bishop::isLegalMove(Position poscurr, Position posnew)