In C++, a struct
and a class
are just the same except with a different default access modifier. struct
members are by default public
, whereas class
members are by default private
. If we think of classes in C++ as being a class
with encapsulated data members and public member functions that expose functionality related to that data, the following are some good suggestions that you want to use a class:
- You need to store some state consisting of related items of data, and...
- You need to impose some invariants on this state or provide some alternative interface to this state.
If you have only suggestion 1, then you probably just want a struct. See Classes Should Enforce Invariants.