Static Member Functions in C++

Like static member variable, we can also have static member functions. A member function that is declared static just place a static keyword before the function header.

Syntax:

static long timediff();

Characteristics of Static Member function:

  1. Static member functions are associated with a class, not with any object.
  2. They can be invoked using class name, not object.
  3. They can access only static members of the class.
  4. They cannot be virtual.
  5. They cannot be declared as constant or volatile.
  6. A static member function can be called, even when a class is not instantiated.   
  7. There cannot be static and non-static version of the same function.
  8. A static member function does not have this pointer.

Example:

#include<iostream.h>
class test
{
	int code;
	static int count; // static member variable
public:
	void set(void)
	{
		code=++count;
	}
	void showcode(void)
	{
		cout<<”object member : “<<code<<end;
	}
	static void showcount(void)
	{ 
		cout<<”count=”<<count<<endl; 
	}
};

Related posts