Free Hosting

Static member variables


Static keyword in C
In the lesson on file scope and the static keyword, you learned that static variables keep their values and are not destroyed even after they go out of scope. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
int GenerateID()
{
    static int s_nIDNo = 0;
    return s_nIDNo++;
}
int main()
{
    std::cout << GenerateID() << std::endl;
    std::cout << GenerateID() << std::endl;
    std::cout << GenerateID() << std::endl;
    return 0;
}
This program prints:
0
1
2
Note that s_nIDNo has kept it’s value across multiple function calls.It will not set it to 0 every time so the static variable do not destroy even the go out of scope.
The static keyword has another meaning when applied to global variables — it changes them from global scope to file scope. Because global variables are typically avoided by competent programmers, and file scope variables are just global variables limited to a single file, the static keyword is typically not used in this capacity.
Static member variables
C++ introduces two new uses for the static keyword when applied to classes: static member variables, and static member function of classes. Before we go into the static keyword as applied to member variables, first consider the following class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Demo
{
private:
    int m_nValue;
public:
    Demo() { m_nValue = 0; }
};
int main()
{
    Demo cFirst;
    Demo cSecond;
    return 0;
}
When we instantiate a class object, each object gets it’s own copy of all normal member variables. In this case, because we have declared two Something class objects, we end up with two copies of m_nValue — one inside cFirst, and one inside cSecond. cFirst->m_nValue is different than cSecond->m_nValue.
Member variables of a class can be made static by using the static keyword. Static member variables only exist once in a program regardless of how many class objects are defined! One way to think about it is that all objects of a class share the static variables. Consider the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Demo
{
public:
    static int s_nValue;
};
int Something::s_nValue = 1;
int main()
{
    Demo cFirst;
    cFirst.s_nValue = 2;
    Demo cSecond;
    std::cout << cSecond.s_nValue;
    return 0;
}
This program produces the following output:
2
Because s_nValue is a static member variable, s_nValue is shared between all objects of the class. Consequently, cFirst.s_nValue is the same as cSecond.s_nValue. The above program shows that the value we set using cFirst can be accessed using cSecond!
Although you can access static members through objects of the class type, this is somewhat misleading. cFirst.s_nValue implies that s_nValue belongs to cFirst, and this is really not the case. s_nValue does not belong to any object. In fact, s_nValue exists even if there are no objects of the class have been instantiated!
Consequently, it is better to think of static members as belonging to the class itself, not the objects of the class. Because s_nValue exists independently of any class objects, it can be accessed directly using the class name and the scope operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Demo
{
public:
    static int s_nValue;
};
int Demo::s_nValue = 1;
int main()
{
    Demo::s_nValue = 2;
    std::cout << Demo::s_nValue;
    return 0;
}
In the above snippet, s_nValue is referenced by class name rather than through an object. Note that we have not even instantiated an object of type Demo, but we are still able to access and use Demo::s_nValue. This is the preferred method for accessing static members.
Initializing static member variables
Because static member variables are not part of the individual objects, you must explicitly define the static member if you want to initialize it to a non-zero value. The following line in the above example initializes the static member to 1:
1
int Demo::s_nValue = 1;
This initializer should be placed in the code file for the class (eg. Something.cpp). In the absense of an initializing line, C++ will initialize the value to 0.
An example of static member variables
Why use static variables inside classes? One great example is to assign a unique ID to every instance of the class. Here’s an example of that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Demo
{
private:
    static int s_nIDGenerator;
    int m_nID;
public:
    Demo() { m_nID = s_nIDGenerator++; }
    int GetID() const { return m_nID; }
};
int Demo::s_nIDGenerator = 1;
int main()
{
    Demo cFirst;
    Demo cSecond;
    Demo cThird;
    using namespace std;
    cout << cFirst.GetID() << endl;
    cout << cSecond.GetID() << endl;
    cout << cThird.GetID() << endl;
    return 0;
}
This program prints:
1
2
3
Because s_nIDGenerator is shared by all Something objects, when a new Something object is created, it’s constructor grabs the current value out of s_nIDGenerator and then increments the value for the next object. This guarantees that each Something object receives a unique id (incremented in the order of creation). This can really help when debugging multiple items in an array, as it provides a way to tell multiple objects of the same class type apart!
Static member variables can also be useful when the class needs to utilize an internal lookup table (eg. to look up the name of something, or to find a pre-calculated value). By making the lookup table static, only one copy exists for all objects, rather than a copy for each object instantiated. This can save substantial amounts of memory.

0 comments:

Blogger Template by Clairvo