提问者:小点点

如何在C++中获得“静态纯虚拟”功能? [已关闭]


我在编程国际象棋,我想对每一个不同的棋子使用抽象的类:

enum class Player { Black=0, White }

class Piece {
protected:
    Player side;
    ...
public:
    // I'm using a 1D array of ints to store the board,
    // cast() returns a list of positions where the piece can go.
    virtual std::vector<int> cast() = 0;
    ...
}

class Bishop {
public:
    // implements cast()
    std::vector<int> cast() override;
    ...
}

如果我想计算出当前玩家的国王不在控制范围内时的每一个位置,那么这个操作就很好。 但是,如果king处于check中,我想要使用的方法是尝试移动king(works),或者计算出piece可以到达的阻止check的方块,然后cast(int position)将所有不同类型的Pieck与该位置一起,并检查是否有一个Pieck可以阻止check。 因此,我要使强制转换静态,但也要使虚拟


共1个答案

匿名用户

这不可能。

对于虚函数,所调用的确切函数取决于调用该函数的对象的类型。

对于静态成员函数,没有对其进行调用的对象,只有一个类型。

因此,两者兼而有之没有什么意义。 编译器如何知道您要调用哪个派生类型?

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|c++|中|静态|虚拟|功能|关闭)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?