Home C++ Tutorial Static Member Function in C++

Static Member Function in C++

by Anup Maurya
3 minutes read

In this tutorial, we learn about static Member Function in C++, what it is, why we need it and how to use it.

What is Static Member Function in C++?

In C++ classes, a static member is a class member that does not belong to object of a class. Just like a static variable. You will have only on copy of that member function irrespective of the number of objects you create of a class.

When a class function is defined as static, all the objects share a static class member. You can even access static function without creating any class object, that why they become independent of class object.

Defining Static Functions in C++

static int learnstaticfunc(void)
{
  cout<<"Learning About Static Function In C++";
}

In this above, example you can see that we have defined a function learnstaticfunc , with a keyword static.

Static Member Function in C++

When a function member is declared static, it becomes independent of other objects in the class. You can call a static member function even if no other class objects exist.

To access class names, you should use the name of the class and the scope resolution operator (::). A static function can only access other static functions, static data members, and other functions outside the class. The scope of static member functions lies within the class, and they cannot access this class pointer.

#include<iostream>
using namespace std;
class BoxClass {
public:
	static int count;
	BoxClass(double l = 2.0, double b = 2.0, double h = 2.0) {
		cout << "Class constructor called." << endl;
		length = l;
		breadth = b;
		height = h;
		count++;
	}
	double getVolume() {
		return length * breadth * height;
	}
	static int getCount() {
		return count;
	}
private:
	double length;     
	double breadth;    
	double height;     
};
int BoxClass::count = 0;
int main(void) {
	cout << "Inital value of count is : " << BoxClass::getCount() << endl;
	BoxClass Box1(3.2, 1.4, 1.8);    
	BoxClass Box2(7.5, 4.0, 4.0);   
	cout << "Final value of count is : " << BoxClass::getCount() << endl;
	return 0;
}

Accessing Static Functions

You don’t need to create a class object to access a static function. Instead, you can use the class name and scope resolution operator (::).

#include<iostream> 
using namespace std;
class MyClass {
public:
	static void msgFunc() {
		cout << "Welcome to World of Learning!";
	}
};
int main() {
	MyClass::msgFunc();
}

Accessing Static Variables

Static variables belong to a class rather than to class objects. If a static variable is public, it’s accessible using the class name and using the scope resolution operator. However, this is not possible if a static member is private.

Normally, private variables are accessed using public functions. However, a class instance/object must be created. The solution is to use a static function.

Static variable in C++ class

#include<iostream> 
using namespace std;
class AClass {
private:
	static int myvar;
public:
	static int getVar() { 
		return myvar; 
		} 
};
int AClass::myvar = 23; 
int main() {
	cout <<"The value of myvar is: "<< AClass::getVar() << '\n';
}

related posts

Leave a Comment

Enable Notifications OK No thanks