– When the static keyword is used inside a class definition, it indicates that the members are not tied to the class objects. This is particularly useful when we want to provide only a single copy of a class variable between all the objects of a class, rather than having each object maintain its own copy.
– As static data members are not bound to class objects, they exist even if no class objects are created.
Example
– A static data member of a class could be used to keep track of the number of class objects that are created.
– A static data member could be a pointer pointing to the error-handling or logging function for that class.
Accessing a static data members
Defining a static data members
– When a function is declared as static inside a class definition, it is not bound to any class object.
– As a static member function is not bound to any class object, it does not receive the this pointer.
Key points
Static Data Members | Static Member Functions |
---|---|
- Data to be shared by all objects of a class, is stored in static data members. Thus static variable is a class variable. - Only a single copy of the static data member exists. - Static data member could be defined inside the public or the private section of a class. - For declaration a static data member is preceded with the keyword static. |
- Static member functions can only access static data members. A static member function is called using the class name i.e Class_Name :: static_function - this pointer never gets passed to a static member function. - As static member functions never receive the this pointer, they cannot access non-static data members. |
Below C++ program demonstrates the use of static data members and static member functions of a class.
Class definition : Employee.h
#include<iostream>
#include<string>
class Employee {
private:
int m_emp_id;
std :: string m_emp_name;
public:
// Declaration of a static member of a class. It needs to be defined in the cpp file.
static std :: string m_company;
Employee (int id, std :: string emp_name) : m_emp_id (id), m_emp_name(emp_name)
{}
void Employee_Details();
static void Org_Details(); // Static member function
};
Employee.cpp
#include "Employee.h"
// Static data member m_company is defined here (i.e in this cpp file)
std :: string Employee :: m_company = "Algotree.org";
void Employee :: Employee_Details() {
std :: cout << "Employee Id : " << m_emp_id << std :: endl;
std :: cout << "Employee Name : " << m_emp_name << std :: endl;
}
void Employee :: Org_Details() {
std :: cout << "Organization Name (displayed using static function) : " << m_company << std :: endl;
}
int main() {
Employee e(25, "Deltoid");
e.Employee_Details();
Employee :: Org_Details();
std :: cout << "Organization Name (displayed using class name) : " << Employee :: m_company << std :: endl;
return 0;
}
Output
Employee Id : 25
Employee Name : Deltoid
Organization Name (displayed using static function) : Algotree.org
Organization Name (displayed using class name) : Algotree.org