Suppose I had two classes like this:
- class myClass {
-
public:
-
static void myStatFunc() const { std::cout << "This is a static function!" << std::endl; }
-
virtual void myVirtFunc() const { std::cout << "Hello, User!" << std::endl; }
-
};
-
-
class myNewClass: public myClass {
-
public:
-
static void myStatFunc() const {std::cout << "This is a static function!" << std::endl; }
-
virtual void myVirtFunc() const { std::cout << "What's up?" << std::endl; }
-
};
A static function is one that is defined only one time. It is universal to the whole class. So if I wanted to call the static function from myClass, I'd type myClass::myStatFunc()
The virtual keyword forces the computer to check the actual version of an object to see which class's function to call. So if I had the following object:
- myClass *myClassPointer = new myNewClass;
and called myClassPointer->myVirtFunc(), even though myClassPointer is technically a myClass object, the myNewClass function is called - that is, "What's up?" is printed.
Now, suppose a static function was virtual. What would be the point? You would still call it by typing myClass::myStatFunc() or myNewClass::myStatFunc(), and the type would be determined by the left-hand side of the :: operator. virtual functions are still dependent on a certain object of a class; static functions are universal to the classes. Thus, the concepts cannot be used on the same function (though both static and virtual functions can be included in the same class, as shown above).