Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Can static functions be virtual in C++?

  • Difficulty Level : Easy
  • Last Updated : 21 Jun, 2017

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

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.




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


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




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


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!