I am new to C++.
I have a class like this:
class CodeTest
{
private:
static const int TOTAL=100;
};
Is TOTAL
a declaration or a definition?
When I was reading Scott Meyer's book, it was mentioned that in the implementation file we need to define something like:
const int CodeTest::TOTAL;
Why is this required?
Static variables in a class: As the variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects.
Static variables are the variables which once declared, get destroyed only when the program has completed its execution. They have the property of retaining their previous scope value if they are already declared once in the program.
Class variablesClass variablesIn class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not an instance variable. It is a special type of class attribute (or class property, field, or data member).https://en.wikipedia.org › wiki › Class_variableClass variable - Wikipedia are also known as static variables, and they are declared outside a method, with the help of the keyword 'static'. Static variable is the one that is common to all the instances of the class. A single copy of the variable is shared among all objects.
Static variables can be defined inside or outside the function. They are local to the block. The default value of static variables is zero. The static variables are alive till the execution of the program.
The declaration in an implementation file outside of the header is required because otherwise every translation unit that includes this header would define its own object (that is, its own storage for the variable).
This would violate the One Definition Rule. A consequence would be e.g. that if the variable was changed in one translation unit, this change would be invisible to other translation units. Now, this isn’t that relevant since the variable is constant. However, taking its address would also yield different pointers in different translation units.
Since this stirred up some controversy, I looked in the standard, and @Nawaz is right, I was wrong.
9.4.2/2
If a
static
data member is of const integral type [...]. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.
So what you have there is a declaration, and the variable is initialized to a value. Outside the class you must define the variable, but not assign a value to it.
The part with const
integral type only applies to this particular case - i.e. you can initialize said type inside the class, but all static
data members must be defined outside.
Regardless of whether the definition is or isn't required outside the class (depending on whether you use the member or not), whatever is inside the class (initialized or not) is just a declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With