Can static functions be virtual in C++?

In C++, a static member function of a class cannot be virtual. For example, below program gives compilation error.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
  
using namespace std;    
   
class Test
{
   public:
      // Error: Virtual member functions cannot be static      
      virtual static void fun()  { }
};

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
  
using namespace std;    
   
class Test
{
   public:
      // Error: Static member function cannot be const
      static void fun() const { } 
};

chevron_right


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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


8


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.