- according to https://google.github.io/styleguide/cppguide.html#Variable_Names, Data members of classes, both static and non-static, are named like ordinary nonmember variables, but with a trailing underscore.
- according to https://google.github.io/styleguide/cppguide.html#Constant_Names, "Variables declared constexpr or const, and whose value is fixed for the duration of the program, are named with a leading "k" followed by mixed case." -- note the "for the duration of the program" part.
- according to https://google.github.io/styleguide/cppguide.html#Access_Control "Make classes' data members private, unless they are constants. This simplifies reasoning about invariants, at the cost of some easy boilerplate in the form of accessors (usually const) if necessary."
So... I think this leaves a bit of room for interpretation, and I wanted to know if anybody knows what the spirit of the standard is...
class A {
const int size_;
public:
A(int size) : size_(size) {}
};
- Is the trailing underscore a sign of "private"-ness or "class"-ness?
- Is the const field supposed to be "kSize" or "size_"?
- If it is moved to be public, should it be "size"?
In your example:
This member variable is not a "constant" for the purposes of the style guide. Its value cannot be changed after construction, but is different per instance. A "constant" inside a class would be
constexpr
orstatic const
orenum
. As it stands, it is not a constant so does not get ak
prefix.To answer your specific questions individually:
size_
because it's not a "constant" in terms of the style guide.Finally, note that
const
member variables inhibit the compiler-generated assignment operator, which is one reason you don't see them that often.