It is similar. Of course, you can use the C way in C++ as well.
But the specific C++ way (a class member) also differs in visibility and scope. Only code that is part of class Cpp
(or a friend) can access Avar
. The PublicAvar
is somewhere in between; anyone can access it, but they need to refer to it as Cpp.PublicAvar
.
The keyword static
has two distinct meanings in C++. Sometimes it means the storage class (as in your example) - that is, "this is not a per-instance member". In this respect, a variable which is not a member of a class at all is obviously static without having to declare that.
Other times, static
may refer to link-time visibility. A static symbol is only valid inside its own compilation unit. That's the original C meaning. It does not come into your example especially because we are assuming that your code goes in a header file, and is therefore present in multiple compilation units. Marking a non-member variable as static
would result in a number of copies of the variable in the program (one per compilation unit) which would not hurt for a const
variable, but generally it could lead to very confusing program behavior.