c++ - 如何调用另一个类的函数?

标签 c++ function oop friend

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);
};

我希望我的 isLegalMove 的 Queen 类实现能够调用函数 isLegalBishopMove。我该如何解决这个问题?我尝试使用 friend ,但没有用。我不理解 C++ 引用资料。

最佳答案

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)

关于c++ - 如何调用另一个类的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584049/

相关文章:

ios - 如何限制我的对象在 iOS 中只调用一次方法?

php - PHP 类中 protected/private 的重要性

c++ - WIndows API 中的 'L' 和 'LPCWSTR'

c++ - clapack.h 或 Visual Studio 2008 中的模板数值工具包

c++ - 用 C++ 构建一个 23 的牙签游戏

OOP - 将一个对象传递给其他几个对象?

c++ - 设计具有部分通用实现的 C++ 类

c++ - Visual C++ 中与 reverse_iterator 相关的崩溃。是合法代码吗?

c++ - 使用另一个对象作为构造函数中的参数创建一个对象

javascript - 函数循环的可能解决方案