Can static functions be virtual in C++?

2.3

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

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

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 25 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.