There is no such thing as "overriding a variable", whether protected or otherwise.
In your example program, the class ChessBoard
contains a member sub object pieces
and a base sub object Board
that contains the member Board::pieces
. Thus, there are two vectors inside the object.
Object oriented programming is achieved through virtual functions. You haven't explained what you're trying to do (beyond something that cannot be done) so this is just a guess, but maybe you are looking for covariant return types:
class Board
{
protected:
virtual Piece&
operator[](std::size_t index) = 0;
public:
virtual ~Board() = default;
};
class ChessBoard : public Board
{
std::vector<ChessPiece> pieces;
protected:
virtual ChessPiece&
operator[](std::size_t index) override;
};
Furthermore, if the sub classes have identical base and/or member function implementations except for the types, then you can avoid repetition by using a template.