C++ Static Data Members
Static data members are class members that are declared using static keywords. A static member has certain special characteristics which are as follows:
- Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
- It is initialized before any object of this class is created, even before the main starts outside the class itself.
- It is visible can be controlled with the class access specifiers.
- Its lifetime is the entire program.
Syntax
className {
static data_type data_member_name;
.....
}
Static data members are useful for maintaining data shared among all instances of a class. The C++ Course explains how to implement static data members, ensuring you understand their significance in C++ programming.
Example
Below is the C++ program to demonstrate the working of static data members:
// C++ Program to demonstrate the use of
// static data members
#include <iostream>
using namespace std;
// class definition
class A {
public:
// static data member here
static int x;
A() { cout << "A's constructor called " << endl; }
};
// we cannot initialize the static data member inside the
// class due to class rules and the fact that we cannot
// assign it a value using constructor
int A::x = 2;
// Driver code
int main()
{
// accessing the static data member using scope
// resultion operator
cout << "Accessing static data member: " << A::x
<< endl;
return 0;
}
Output
Accessing static data member: 2
Defining Static Data Member
As told earlier, the static members are only declared in the class declaration. If we try to access the static data member without an explicit definition, the compiler will give an error.
To access the static data member of any class we have to define it first and static data members are defined outside the class definition. The only exception to this are static const data members of integral type which can be initialized in the class declaration.
Syntax
datatype class_name::var_name = value...;
For example, in the above program, we have initialized the static data member using the following statement:
int A::x = 10
Note: The static data members are initialized at compile time so the definition of static members should be present before the compilation of the program
Inline Definition of Static Data Member since C++ 17
The C++ 17 introduced the inline definition of the static data members of type integral or enumeration which was not allowed in the previous standards. This simplifies the definition of the static data members.
Syntax
// inside class definition
...
static inline data_type var_name = value/expression;
....
Accessing a Static Member
We can access the static data member without creating the instance of the class. Just remember that we need to initialize it beforehand. There are 2 ways of accessing static data members:
1. Accessing static data member using Class Name and Scope Resolution Operator
The class name and the scope resolution operator can be used to access the static data member even when there are no instances/objects of the class present in the scope.
Syntax
Class_Name :: var_name
Example
A::x
2. Accessing static data member through Objects
We can also access the static data member using the objects of the class using dot operator.
Syntax
object_name . var_name
Example
obj.x
Note: The access to the static data member can be controlled by the class access modifiers.
Example to Verify the Properties of the Static Data Members
The below example verifies the properties of the static data member that are told above:
// C++ Program to demonstrate
// the working of static data member
#include <iostream>
using namespace std;
// creating a dummy class to define the static data member
// it will inform when its type of the object will be
// created
class stMember {
public:
int val;
// constructor to inform when the instance is created
stMember(int v = 10): val(v) {
cout << "Static Object Created" << endl;
}
};
// creating a demo class with static data member of type
// stMember
class A {
public:
// static data member
static stMember s;
A() { cout << "A's Constructor Called " << endl; }
};
stMember A::s = stMember(11);
// Driver code
int main()
{
// Statement 1: accessing static member without creating
// the object
cout << "accessing static member without creating the "
"object: ";
// this verifies the independency of the static data
// member from the instances
cout << A::s.val << endl;
cout << endl;
// Statement 2: Creating a single object to verify if
// the seperate instance will be created for each object
cout << "Creating object now: ";
A obj1;
cout << endl;
// Statement 3: Creating multiple objects to verify that
// each object will refer the same static member
cout << "Creating object now: ";
A obj2;
cout << "Printing values from each object and classname"
<< endl;
cout << "obj1.s.val: " << obj1.s.val << endl;
cout << "obj2.s.val: " << obj2.s.val << endl;
cout << "A::s.val: " << A::s.val << endl;
return 0;
}
Output
Static Object Created
accessing static member without creating the object: 11
Creating object now: A's Constructor Called
Creating object now: A's Constructor Called
Printing values from each object and classname
obj1.s.val: 11
obj2.s.val: 11
A::s.val: 11
Note: In C++, we cannot declare static data members in local classes.