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.
#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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Static functions in C
- C++ | Virtual Functions | Question 3
- C++ | Virtual Functions | Question 13
- C++ | Virtual Functions | Question 12
- C++ | Virtual Functions | Question 6
- C++ | Virtual Functions | Question 14
- C++ | Virtual Functions | Question 9
- C++ | Virtual Functions | Question 10
- C++ | Virtual Functions | Question 7
- C++ | Virtual Functions | Question 5
- C++ | Virtual Functions | Question 4
- C++ | Virtual Functions | Question 11
- C++ | Virtual Functions | Question 14
- Can virtual functions be private in C++?
- C++ | Virtual Functions | Question 2

