Open In App
Related Articles

Some interesting facts about static member functions in C++

Improve Article
Improve
Save Article
Save
Like Article
Like

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 “

CPP




#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

CPP




#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 ” 

CPP




#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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Oct, 2020
Like Article
Save Article
Previous
Next
Similar Reads