1. Static Function
Static member functions have a class scope and they do not have access to the 'this' pointer of the class. When a member is declared as static, a static member of class, it has only one data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated.
static return_type function_name()
Calling Static Function in the main Function
class_name::static function name();
static int countt;
static void statfun()
count c1,c2,c3,c4,c5; clrscr();
count::statfun();//calling the constructor using classname
getch();//scope resolution operator and the static funtion name
The programmer must note the following while using static member functions:
A static member function can only access static member data, static member functions and data and functions outside the class. The programmer must take note not to use static member function in the same manner as non-static member function, as non-static member function can access all of the above including the static data member.
· A non-static member function can be declared as virtual but care must be taken not to declare a static member function as virtual.
· The programmer must first understand the concept of static data while learning the context of static functions. It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once. Non-static data members are created again and again. For each separate object of the class, the static data is created and initialized only once. As in the concept of static data, all objects of the class in static functions share the variables. This applies to all objects of
· A non-static member function can be called only after instantiating the class as an object. This is not the case with static member functions. A static member function can be called, even when a class is not instantiated.
· A static member function cannot have access to the 'this' pointer of the class.
2. Inline member Function
We may either define a member function inside its class definition, or you may define it outside if you have already declared (but not defined) the member function in the class definition.
A member function that is defined inside its class member list is called an inline member function. Member functions containing a few lines of code are usually declared inline. In the above example, add() is an inline member function. If you define a member function outside of its class definition, it must appear in a namespace scope enclosing the class definition. You must also qualify the member function name using the scope resolution (::) operator.
An equivalent way to declare an inline member function is to either declare it in the class with the inline keyword (or define the function outside of its class) or to define it outside of the class declaration using the inline keyword.