How should I order the members of a C++ class?

后端 未结 15 1766
Happy的楠姐
Happy的楠姐 2020-12-04 11:50

Is it better to have all the private members, then all the protected ones, then all the public ones? Or the reverse? Or should there be multiple private, protected and pub

15条回答
  •  不思量自难忘°
    2020-12-04 12:21

    i think it's all about readability.

    Some people like to group them in a fixed order, so that whenever you open a class declaration, you quickly know where to look for e.g. the public data members.

    In general, I feel that the most important things should come first. For 99.6% of all classes, roughly, that means the public methods, and especially the constructor. Then comes public data members, if any (remember: encapsulation is a good idea), followed by any protected and/or private methods and data members.

    This is stuff that might be covered by the coding standards of large projects, it can be a good idea to check.

提交回复
热议问题