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() { } }; |
chevron_right
filter_none
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 { } }; |
chevron_right
filter_none
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.
Recommended Posts:
- Can virtual functions be private in C++?
- Can virtual functions be inlined?
- Understanding "static" in "public static void main" in Java
- Difference between static and non-static method in Java
- Difference between static and non-static variables in Java
- Internal static variable vs. External static variable with Examples in C
- Understanding storage of static methods and static variables in Java
- Why non-static variable cannot be referenced from a static method in Java
- What happens when a virtual function is called inside a non-virtual function in C++
- Difference between Virtual function and Pure virtual function in C++
- Some interesting facts about static member functions in C++
- Virtual functions in derived classes
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- Pure Virtual Functions and Abstract Classes in C++
- Why can't static methods be abstract in Java?
- Templates and Static variables in C++
- Comparison of static keyword in C++ and Java
- Static data members in C++
- When are static objects destroyed?
- C++ | Static Keyword | Question 1

