Member function can be called without creating any object. const empty static virtual

The storage class specifier static can be used in class declarations of data and function members. Such members are called static members and have distinct properties from nonstatic members. With nonstatic members, a distinct copy exists for each instance of the class; with static members, only one copy exists, and it can be accessed without reference to any particular object in its class. If x is a static member of class X, it can be referenced as X::x (even if objects of class X have not been created yet). It is still possible to access x using the normal member access operators. For example, y.x and yptr->x, where y is an object of class X and yptr is a pointer to an object of class X, although the expressions y and yptr are not evaluated. In particular, a static member function can be called with or without the special member function syntax:

class X {
   int member_int;
public:
   static void func(int i, X* ptr);
};
void g(void)
{
   X obj;
   func(1, &obj);      // error unless there is a global func()
                       // defined elsewhere
   X::func(1, &obj);   // calls the static func() in X
                       // OK for static functions only
   obj.func(1, &obj);  // so does this (OK for static and
                       // nonstatic functions)
}

Because static member functions can be called with no particular object in mind, they do not have a this pointer, and therefore cannot access nonstatic members without explicitly specifying an object with . or ->. For example, with the declarations of the previous example, func might be defined as follows:

void X::func(int i, X* ptr)
{
   member_int = i;       // which object does member_int
                         // refer to? Error
   ptr->member_int = i;  // OK: now we know!
}

Apart from inline functions, static member functions of global classes have external linkage. Static member functions cannot be virtual functions. It is illegal to have a static and nonstatic member function with the same name and argument types.

The declaration of a static data member in its class declaration is not a definition, so a definition must be provided elsewhere to allocate storage and provide initialization.

Static members of a class declared local to some function have no linkage and cannot be initialized. Static members of a global class can be initialized like ordinary global objects, but only in file scope. Static members, nested to any level, obey the usual class member access rules, except they can be initialized.

class X {
   static int x;
   static const int size =  5;
   class inner {
      static float f;
      void func(void);     // nested declaration
      };
public :
   char array[size];
};
int X::x = 1;
float X::inner::f = 3.14;  // initialization of nested static
void X::inner::func(void) {     /*  define the nested function */  }

The principal use for static members is to keep track of data common to all objects of a class, such as the number of objects created, or the last-used resource from a pool shared by all such objects. Static members are also used to

In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you have a pointer or reference to an instance of a class. Static functions aren’t tied to the instance of a class but they are tied to the class. C++ doesn’t have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.

For example, below program gives compilation error,

CPP




// CPP Program to demonstrate Virtual member functions

// cannot be static

#include <iostream>

 

using namespace std;

 

class Test {

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
0
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
1

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
2
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
3
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
4
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
5
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
6

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
7

Output

prog.cpp:9:29: error: member ‘fun’ cannot be declared both virtual and static
    virtual static void fun() {}
                            ^

Also, static member function cannot be const and volatile. Following code also fails in compilation,

CPP




prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
8

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
9

#include <iostream>

 

using namespace std;

 

class Test {

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
0
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
1

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
2
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
4
prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
5 // cannot be static1// cannot be static2 // cannot be static3

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^
7

Output

prog.cpp:8:23: error: static member function ‘static void Test::fun()’ cannot have cv-qualifier
    static void fun() const {}
                      ^

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Does member function can be called without creating any object?

Explanation: The member functions can be called using only the dot operator or the arrow operator. But the static members can be called using directly the class name followed by the scope resolution operator and static member function name. This is useful when you don't have any object to call the member.

Can we call any class member function without using object of the class?

Like static data members, you may access a static member function f() of a class A without using an object of class A .

Can a static function call a member function?

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

What is the member function that is called when an object is created?

A constructor is a special type of member function that is called to create a new object of the class. The usual job of a constructor is to initialize the data members of a new object to valid values. The name of a constructor is always the same as the name of the class.