views:

41

answers:

1

What should go into a class declaration in C++?

For example, I have the following in a header file:

class BoardState {
 public:
  BoardState();

  bool HasWon() const;
  bool HasMoves() const;
  bool MakeMove(const int column);
  bool UndoMove(const int column);

  const Chip (&grid() const)[kGridHeight][kGridWidth] { return grid_; }
  const Chip lastplayer() const { return lastplayer_; }

 private:
  Chip grid_[kGridHeight][kGridWidth];
  Chip turn_;
  Chip lastplayer_;
  int lastmove_;
  DISALLOW_COPY_AND_ASSIGN(BoardState);
};

The cpp file for this class defines many additional small utility functions, types, and enums. Should all of these also be defined in the private section of the class declaration?

A: 

It is usually best to leave auxiliary functionality outside the class. That way, you can overload the functions for similar classes and similar functionality.

Small utilities which are part of the implementation and won't/shouldn't do anything for any other component, though, should be private members.

Potatoswatter