Can we have a virtual static method ? (c++) [duplicate]
Solution 1:
No. static
on a function in a class means that the function doesn't need an object to operate on. virtual
means the implementation depends on the type of the calling object. For static there is no calling object, so it doesn't make sense to have both static
and virtual
on the same function
.
Solution 2:
Don't think this is possible because you could call A::F();
without having the object A.
Making it virtual and static would mean a contradiction.
Solution 3:
Because the class doesn't have a this
pointer. In there is the virtual function lookup table. A quick google can tell you more about the virtual function lookup table.
Solution 4:
No, static
function is like global function, but also inside class namespace. virtual
implies inheritance and reimplementing in derived class - you can't reimplement 'global' function.