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

后端 未结 15 1799
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

    Google favors this order: "Typedefs and Enums, Constants, Constructors, Destructor, Methods, including static methods, Data Members, including static data members."

    Matthew Wilson (Safari subscription required) recommends the following order: "Construction, Operations, Attributes, Iteration, State, Implementation, Members, and my favorite, Not to be implemented."

    They offer good reasons, and this kind of approach seems to be fairly standard, but whatever you do, be consistent about it.

提交回复
热议问题