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.

 1 #include<iostream>
 2 using namespace std;    
 3 
 4 class Test
 5 {
 6 public:
 7     // Error: Virtual member functions cannot be static      
 8     virtual static void fun()  
 9     { 
10     }
11 };

  


  Also, static member function cannot be const and volatile.

  Following code also fails in compilation.

 1 #include<iostream>
 2  
 3 using namespace std;    
 4   
 5 class Test
 6 {
 7    public:
 8       // Error: Static member function cannot be const
 9       static void fun() const 
10       { 
11       } 
12 };

 

 

 

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

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  20:31:21

posted @ 2013-11-26 20:32  虔诚的学习者  阅读(353)  评论(0编辑  收藏  举报