Some interesting facts about static member functions in C++

1) static member functions do not have this pointer.
For example following program fails in compilation with error “`this’ is unavailable for static member functions “

#include<iostream>
class Test {      
   static Test * fun() {
     return this; // compiler error
   }
};
 
int main()
{
   getchar();
   return 0;
}

2) A static member function cannot be virtual (See this G-Fact)

3) Member function declarations with the same name and the name parameter-type-list cannot be overloaded if any of them is a static member function declaration.
For example, following program fails in compilation with error “‘void Test::fun()’ and `static void Test::fun()’ cannot be overloaded

#include<iostream>
class Test {
   static void fun() {}
   void fun() {} // compiler error
};
 
int main()
{
   getchar();
   return 0;
}

4) A static member function can not be declared const, volatile, or const volatile.
For example, following program fails in compilation with error “static member function `static void Test::fun()’ cannot have `const’ method qualifier ”

#include<iostream>
class Test {      
   static void fun() const { // compiler error
     return;
   }
};
 
int main()
{
   getchar();
   return 0;
}

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

References:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf





  • poonam tiwari

    why it is appropriate to say “c++” compair to “better c”?

  • Anonymous

    Simple Program That shows Logic of Using Static Functions
    http://geeksprogrammings.blogspot.com/2013/09/static-member-functions.html

  • muthua

    why Member function declarations with the same name and the name parameter-type-list cannot be overloaded if any of them is a static member function declaration.

  • Javed

    I think this should also be added here:
    Since static member functions do not have ‘this’ pointer, they can not access any non-static data member and can not call any non-static member function.

  • ysr_shk

    What is the difference between class and structure?

    structure also have inheritance,object overloading,overriding,access specifiers,scope resolution operator…etc.
    The things that doesn’t work with structure is only abstract and virtual.
    But I don’t think so,,,for that only they’ve invented class.

    Thanking you.

    • gandhi_rahul

      1. Members of Structure are by default Public in nature whereas in class they are private.
      2. When deriving a Struct from some base class/structure, the default access specifier for a base class/structure is public.
      While deriving a class, the access specifier is private.
      3. Also, Classes can have explicitly parameterless constructors whereas structures can’t.
      4. Member variable initialization is possible in class whereas in Structures, it is not.
      5. It is not possible to declare destructor in structure but in class it is possible.
      6. The “this” keyword gives different meaning in structure and class. How?
      In class, “this” keyword is classified as value type of class type within which it is used like inside instance constructor or instance method.
      In structure, “this” keyword is classified as variable type of structure type within which it is used.
      7.One more main difference is that the structures are value types and the classes are reference types.So object of structure store in stack exactly like any other value type like an Integer, a double
      but object of class store in heap.You can assigned null to class variable but you can’t assigned null value to structure variable

  • AKS

    Why static member functions cannot be declared const, volatile, or const volatile??

    • http://gansai.blogspot.com gansai

      If Static member functions are declared const, then it means that our intention is like: we have access to a particular object and we are not allowed to modify the contents of that object. So, since we dont have access to a particular object in a static member function, we cannot declare it const.

      If static member functions are declared volatile,
      we have access to a volatile( or non-volatile ) object and the compiler is not allowed to optimize code in this function which shall affect the state of that object because of its volatility. But, since static member functions do not have access to a particular instance of a class, then this situation need not arise. So, static member functions can’t be declared volatile.
      So, on a side-note, non-static member functions can be declared const volatile.

  • Abhimanyu Vohra

    why static member function cannot be virtual?

    • kartik

      static member functions can be called without any object. So vptr (virtual pointer table) can’t be made available to them. vptr is set in constructor only.

      • Abhimanyu Vohra

        Thanks a lot!! Actually, i got confused with the comments section of G-Fact in point 2.

  • Ankit

    Nice article

  • Arun

    What is in c that is not in c++?

    • prashanth

      @Arun,
      What is in c that is not in c++?

      Answer:
      int new;

  • geek4u

    static member functions do not have this pointer.
    static member functions do not need this pointer bnecause they are class specific functions, not object specific functions. They are shared among all objects.

    A static member function cannot be virtua
    Since this pointer is not present with functions, they cannot access VPTR.

    • max

      nice one