What happens when we make virtual function static ? I tried to make a virtual function static in parent class but it was giving compiler error.
Asked
Active
Viewed 394 times
0
-
Why do you think that there is a need to make a static function virtual? What is the problem you want to solve with that? – t.niese Feb 23 '20 at 08:01
-
What is the behaviour you have in mind that a static or free virtual function would have? – Ulrich Eckhardt Feb 23 '20 at 08:22
-
1Virtual functions are specified to have behaviour that is determined by the actual type of the object the function is called on (i.e. the actual type of the object `a` in the call `a.some_virtual()`) . A static member function of a class can be called without reference to any object of that class, but is accessible to all member of that class. So the requirements of a virtual function and of a static member function of the same class are mutually exclusive. – Peter Feb 23 '20 at 08:43
-
If you try to explain to yourself what a static virtual function would do, you will probably also understand why it does not exist. – super Feb 23 '20 at 09:30
-
1Does this answer your question? [Can we have a virtual static method ? (c++)](https://stackoverflow.com/questions/7227236/can-we-have-a-virtual-static-method-c) – t.niese Feb 23 '20 at 11:09
1 Answers
1
Virtual functions operate by using the current object to look up the correct method to call via its vtable, this is why they can be overridden. Static and global methods do not refer to an object and so has no place to store such a pointer to a function.
You can find out more by searching for info about the vtable.

Wiingreen
- 188
- 7
-
2The standard does not require that objects with virtual functions have a vtable. That is an implementation choice, not a requirement of the standard. The standard specifies how virtual functions behave, not now that behaviour is implemented. – Peter Feb 23 '20 at 08:37
-
I didn't know that, awesome contribution. But I feel that it is easier to explain the limitation using vtables as examples. Do you know any examples of compilers which a not using vtables? – Wiingreen Feb 23 '20 at 08:41
-
`Static and global methods do not refer to an object and so has no place to store such a pointer to a function.` they refer to a class and a class can have static member variables and hench a place to store information, so saying there wouldn't be a place to store such information is not an argument. – t.niese Feb 23 '20 at 08:53
-
1@t.niese there's still no place for object-specific information among these static members. They are by definition common to all the objects of enclosing type, unlike virtual functions, which can be different depending on object's dynamic type. – Ruslan Feb 23 '20 at 08:57
-
That would be true except for virtual methods. Because we do not know the method to call because we could be referencing a child of the class which overrides the method, the pointer to the method cannot be stored at the class anymore. We must at least have a pointer passed which point to the current vtable. – Wiingreen Feb 23 '20 at 08:59