C++ – About the choice of struct and class keywords

It is well known that in C++ struct Derived from the historical legacy of the C language, except that the default scope is public outside, with class There is no difference, in theory they can be mixed. We use the standard library and engineering practices such as Unreal to implement structclass Make a summary of keyword selection.

  • If the type contains something other than public Member variables should be selected class .
  • If the type contains virtual function, should be selected class .
  • If the type can Aggregation initialization , should be considered struct .
  • If the constructor, destructor and assignment function of the type are = default; implementation, should be considered struct .

Post Reply