Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 public labels so that the operations can be kept separate from the constructors and so on? What issues should I take into account when making this decision?

like image 298
Tommy Herbert Avatar asked Nov 21 '08 12:11

Tommy Herbert


People also ask

What is class member in C?

Class members are initialized in constructors which can be overloaded with different signatures. For classes that do not have constructor, a default constructor that initializes the class members (to default values) will be generated. Unlike in C++, C# allows a class to inherit from one base class only.

What's the importance of the order of members in a class definition?

The order is important when a member variable depends on another member variable when using initializer lists: Imagine a class with two variable where the constructor of the second variable (varC) needs the first (varB)

How do you initialize a class member in C++?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

Does order of classes matter C++?

But once you declared them, the order does not matter (or very marginally). For short functions, it might be slightly better to group related functions (eg f before g if g calls f ), perhaps because of cache issues. But this is often irrelevant (and the compiler will often reflush the generated function order).


2 Answers

I put the public interface first, but I didn't always do this. I used to do things backwards to this, with private, then protected, then public. Looking back, it didn't make a lot of sense.

As a developer of a class, you'll likely be well acquainted with its "innards" but users of the class don't much care, or at least they shouldn't. They're mostly interested in what the class can do for them, right?

So I put the public first, and organize it typically by function/utility. I don't want them to have to wade through my interface to find all the methods related to X, I want them to see all that stuff together in an organized manner.

I never use multiple public/protected/private sections - too confusing to follow in my opinion.

like image 189
itsmatt Avatar answered Oct 03 '22 09:10

itsmatt


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.

like image 22
Josh Kelley Avatar answered Oct 03 '22 08:10

Josh Kelley