Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static variable in the class declaration or definition?

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?

like image 744
Whoami Avatar asked Jun 24 '12 14:06

Whoami


People also ask

Can we declare static variable in class?

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.

What is static in variable declaration?

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.

What is a static variable in a class?

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.

Where static variable should be defined?

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.


2 Answers

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.

like image 127
Konrad Rudolph Avatar answered Nov 03 '22 04:11

Konrad Rudolph


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.

To answer the question:

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.

like image 30
Luchian Grigore Avatar answered Nov 03 '22 05:11

Luchian Grigore