C ++-在没有公共方法的情况下访问受保护的成员?

问题描述 投票:0回答:1

所以我有一项学校任务,必须使用继承编写国际象棋程序。这是我当前的代码

// chessmove.h
class ChessPiece;

struct ChessMove {
  int from_x;
  int from_y;
  int to_x;
  int to_y;

  ChessPiece* piece; // you can change the position of the chess piece with this pointer.
};

// chessboard.h
using std::vector;
using std::istream;
using std::string;
class ChessBoard {
  // add additional members or functions of your choice

private:
  Matrix<ChessPiece* > state;

public:
  void move_piece(ChessMove chessMove);
  vector<ChessMove> capturingMoves(bool isWhite);
  vector<ChessMove> nonCapturingMoves(bool isWhite);
};
ChessBoard& operator>>(istream&, ChessBoard&);

// chesspiece.h
class ChessPiece {
  friend void ChessBoard::move_piece(ChessMove p);
protected:
  int x, y;
  bool isWhite;
  ChessBoard* board;
  /**
   * Returns 0 if target square is unreachable.
   * Returns 1 if target square is reachable and empty.
   * Returns 2 if move captures a piece.
   */
  virtual int validMove(int to_x, int to_y);
  virtual char32_t utfRepresentation();
  virtual char latin1Representation();
public:
  /**
   * Checks if this move is valid for this piece and captures
   * a piece of the opposite color.
   */
  bool capturingMove(int to_x, int to_y);
  /**
   * Checks if this move is valid but does not capture a piece.
   */
  bool nonCapturingMove(int to_x, int to_y);
  virtual vector<ChessMove> capturingMoves();
  virtual vector<ChessMove> nonCapturingMoves();

  /**
  * For testing multiple inheritance
  */
  int unnecessaryInt;
};

// king.h
class King : public ChessPiece
{
public:
  int validMove(int to_x, int to_y);
};

// king.cpp
int King::validMove(int to_x, int to_y)
{
  int deltaX = std::abs(this->x - to_x);
  int deltaY = std::abs(this->y - to_y);

  if (((deltaX * deltaY) == 1) || ((deltaX + deltaY) == 1)) 
  {
    /*if (? ? ? ) 
    {
      return 1;
    }*/
  }
  return 0;
}

chessmove.hchessboard.hchesspiece.h代码由学校任务说明给出。 king.hking.cpp是我写的。

我正在尝试覆盖validMove中的king.cpp方法,但我只能检查目标方格是否可达。我无法检查目标正方形是否为空或是否被相对的一块占据(因为isWhitexy成员在ChessPiece中受保护)。我之前尝试过向chessboard.h添加一个方法,该方法以指定的索引返回矩阵state中的指针元素(ChessPiece *),但是当我尝试使用返回的指针来检索前面提到的成员时,它不会我,因为成员受到保护。

[基本上,我目前只能使该方法返回0,但不能返回12,因为我无法检查目标正方形是否为空,也无法检查该块的颜色已占领的目标广场。

我不允许更改给定代码(不是king.hking.cpp的代码)中的任何访问修饰符。在ChessPiece类中,我只能添加私有或受保护的方法,但是在ChessBoard类中,我可以添加我想要的任何方法。

感觉好像我在这里确实缺少了一些明显的东西……帮助?

((我不想提供完整的解决方案。我只想让您考虑一下此或给定的想法。)

谢谢!

编辑:这是我之前尝试(并提到)的代码,该代码不允许访问ChessPiece指针中的受保护成员]

// king.cpp
int King::validMove(int to_x, int to_y)
{
  int deltaX = std::abs(this->x - to_x);
  int deltaY = std::abs(this->y - to_y);

  if (((deltaX * deltaY) == 1) || ((deltaX + deltaY) == 1)) 
  {
    ChessPiece* cp = board->getPieceAtPos(to_x, to_y);
    if (cp == nullptr) 
    {
      return 1;
    }

    else
    {
      if(this->isWhite != cp->isWhite)
      {
        return 2;
      }
      return 1;
    }
  }
  return 0;
}

// bottom of ChessBoard class (chessboard.h)
public:
ChessPiece* getPieceAtPos(int x, int y) {
    return state(x, y);
  }

[this->isWhite工作正常,但对于cp->isWhite,VS表示“'ChessPiece :: isWhite':无法访问在类'ChessPiece'中声明的受保护成员”]

c++ inheritance multiple-inheritance access-modifiers
1个回答
0
投票

缺少的键只有3个状态,不是4个

* Returns 0 if target square is unreachable.
* Returns 1 if target square is reachable and empty.
* Returns 2 if move captures a piece.

[您要做的是将(2)分为两个状态:捕获和非法,但是请注意,没有定义“可被自己的颜色占据和占据”状态,因此(2)必须同时执行。]

[这是措词不精确的一个例子,正如您认为捕获是“合法捕获”一样,而老师认为这是任意的意思。

ChessBoard :: move_piece可以很容易地检查这两个棋子是否是相反的颜色,因此此举是否是真实的,因为它是一个朋友,因此是非法的。当然,这使您无法将此决定封装到另一个方法中。

© www.soinside.com 2019 - 2024. All rights reserved.