Can we have a static virtual functions? If not, then WHY?

Jatin picture Jatin · Mar 25, 2012 · Viewed 64.4k times · Source

Possible Duplicate:
C++ static virtual members?

Can we have a static virtual functions? If not, then WHY?

class X
{
public:
       virtual static void fun(){} // Why we cant have static virtual function in C++?
};

Answer

Oliver Charlesworth picture Oliver Charlesworth · Mar 25, 2012

No, because it doesn't make any sense in C++.

Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.