C 将指针传递到当前对象到一个函数

C++ Passing pointer to current object to a function

本文关键字:函数 一个 对象 指针      更新时间:2023-10-16

这是一个标题问题,我忘了包括一个标题...

我们应该使用getMoves(this)

我需要将指针发送到当前对象到一个函数,但无法设法找出如何做...:/

Chess_piece.h

class ChessPiece
{
    public:
        virtual std::list<Move *> getMoves(Chess *game) = 0;
};

Chess.cpp

list<Move *> Chess::getMoves()
{
    ChessPiece *p;
    ...
    list<Move *> moves = p->getMoves(*this);
    // can't manage to get a pointer to the current object here
}

我在做什么错?如何获取指向当前对象的指针?

thisChess* const

*this给我一个Chess&,当我尝试这样做时:

list<Move *> moves = p->getMoves(*this);

我得到no matching function for call to ‘ChessPiece::getMoves(Chess&)’

编辑

每个人都告诉我这样做:

list<Move *> moves = p->getMoves(this);

但是我已经尝试了,我得到了:

no matching function for call to ‘ChessPiece::getMoves(Chess* const)’

edit2

这是我对国际象棋的声明:: getmoves():

Chess.h

class Chess : public Game
{
    public:
        std::list<Move *> getMoves();
};

getMoves(国际象棋 *game)= 0;

GetMoves将国际象棋类指针作为参数。

list moves = p-> getMoves(*this);

在这里您正在通过 *,但这本身就是指针。

so> list moves = p-> getMoves(this);

相关文章: