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

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

    Put the private fields first.

    With modern IDEs, people don't read the class to figure out what it's public interface is.

    They just use intellisence (or a class browser) for that.

    If someone is reading through the class definition, it's usually because they want to understand how it works.

    In that case, knowing the fields helps the most. It tells you what the parts of the object are.

提交回复
热议问题