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:
- Static member functions are associated with a class, not with any object.
- They can be invoked using class name, not object.
- They can access only static members of the class.
- They cannot be virtual.
- They cannot be declared as constant or volatile.
- A static member function can be called, even when a class is not instantiated.
- There cannot be static and non-static version of the same function.
- 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;
}
};
You must log in to post a comment.