Javatpoint Logo
Javatpoint Logo

C++ Static Variable

What is a Variable?

A variable is a name of a memory location used to store a value that can be modified and reused many times during the program execution.

Example: A car, Name

A variable is a user-defined reference to the memory location storing data.

Values assigned to variables can be of any data type like int, float, char, etc.



There are three types of variables. They are:

  1. Local variables
  2. Global variables
  3. automatic variables
  4. External variables
  5. Static variables

Global Variables

These are the variables declared outside the functions and are called global variables.


Local Variables

These variables are declared inside the functions and are called local variables.


Automatic Variables

These variables are declared with auto keywords and are called automatic variables.


External Variables

External variables are used to share the variables among the multiple C files.


Static Variables

Variables declared with the keywords "Static" are static variables whose lifetime is throughout the program run time.


Let us discuss this briefly.

Non-static (ordinary) Member Variables

Regular member variables of a class exist in every object. That is, when you declare a class and list the member variables, you are saying that every object in the class should have its own space in memory for each member variable.

After declaring Things t1 and t2, each consists of a memory holding two integers, so we have t1.x, t1.y, t2.x, t2.y. Each object has its own individual x and y variables.

Static Member Variables

Sometimes it is handy to have a member variable associated with the whole class instead of individual objects. Such a variable occupies its piece of memory, by itself, instead of being part of an individual object. It is essentially a global variable, but its name is contained inside a class scope, so it goes with the class instead of being known everywhere in the program. Such a member variable can be made private to a class, meaning only member functions can access it. A good name for this property would be something like "class-wide" or "whole-class", but unfortunately, the overworked keyword "static" was used instead, so we have "static member variables".

Here is an example of a Thing class where a static member variable, named "count", is used to keep a count of how many Things exist. Every time a Thing is created, the constructor will increment the count; every time a Thing is destroyed, the destructor will decrement the count. Because the count variable is not part of individual objects but exists for the entire class, it "lives" past the creation and destruction of individual objects, retaining its value while the individual objects come and go.


In this example, the count is a public static member variable whose full Name is Thing::count. Even if it was private, the constructor and destructor member functions could access it because they are member functions. Because the count is static, it is a class-wide member variable, not a member variable of the individual thing objects; there is only one Thing::count variable in the entire program. We have to define and initialize a static member variable somewhere in our code, at the top level (outside of any function), the same way we define and initialize a global variable.

The compiler will ensure that the initialization is done before our program starts executing at the main function. In this definition and initialization line, we provide the full name of the static member variable using the class scope operator: The best place to define the static member variable is near the beginning of the .cpp file that goes with the header file that contains the class declaration. For example, in the file "Thing.h", we have the Thing class declaration, and then in "Thing.cpp," we would have:

Code for Thing Class Member Functions

The "= 0;" is optional; by default, static variables are initialized to whatever type of zero is appropriate, but including it is a customary way of making this definition more obvious. At any point in your code, you can refer to a static member variable either in the context of an object or just with the class scope operator, assuming that the code has access:

We don't have to have an object involved because the static member variable belongs to the whole class. If we access the static member variable with an object, like in "t.count", the only role the object "t" plays is to inform the compiler what class the "count" variable belongs to - count is not part of the object, but belongs to the whole class of objects. Accessing a static member variable through an object is not usually done; the class scope operator form is how it is done because the variable is normally private through a static accessor function.

Defining and Initializing Private Static Member Variables

We can make a static member variable private and often want to. But we can (and must) still define it the same way as shown above, even though it is private. For example, in Thing.h:

And then in Thing.cpp, we have the same thing as when Thing::count was public:

Code for Thing Class Member Functions

Yes, this looks odd because the definition and initialization statement is accessing a private member variable from outside the class. Yes, it is doing just that. But we must be able to define the member variable somehow, even if it is private. So this exception to the access rules is allowed. But now we can no longer refer to the member variable in other ways because it is private:

The usual solution is to use a static member function to access the private static variable.

Static Member Functions

A static member function is like a static member variable in that we can invoke it without an object being involved. Regular member functions can only be applied to an object and have the hidden "this" parameter.

A static member function is like a normal function, but its name is contained inside the class scope instead of being global to the whole program or translation unit. It allows us to keep a function associated with a class and ensure that the function name doesn't conflict with another function with the same name. And like a normal function, because it does not apply to an individual object, there is no "this" parameter. A key advantage is that it has access to private member variables because it is a class member; naturally, this includes static member variables. So, continuing the example, we can use a static member function as a reader for a private static member variable:

If desired, we can also define the function outside the class declaration the same way as a normal member function.

In our code, call the function as follows:

The function get_count is a static member function that returns the value of the static member variable count. Because it is static, it can be called with or without an object being involved. If it is called with an object, as in t.get_count(), the only role that objects "t" plays is to inform the compiler what class get_count belongs to. As with static member variables, accessing a static member function through an object is not usually done. The class scope operator form is normally called a static member function.

An important property of a static member function is that we can't access the ordinary (non-static) members of the class from within the static member function - there is no individual object automatically involved (there is no "this" object). Suppose we need your static member function to access the members of an object in its class. In that case, we have to pass in the object as a parameter or provide some other access (e.g., through a global variable or static member variable). Because the static member function is a class member, it can access all members of the passed-in object.

Code for Static Variable inside of a Class


C++ Static Variable

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA