Why is overriding of static methods left out of most OOP languages?

一曲冷凌霜 提交于 2019-12-04 20:44:24

Speaking to C++

class Foo {
public:
    static  void staticFn(int i);
    virtual void virtFn(int i);
};

The virtual function is a member function - that is, it is called with a this pointer from which to look up the vtable and find the correct function to call.

The static function, explicitly, does not operate on a member, so there is no this object from which to look up the vtable.

When you invoke a static member function as above, you are explicitly providing a fixed, static, function pointer.

foo->virtFn(1);

expands out to something vaguely like

foo->_vtable[0](foo, 1);

while

foo->staticFn(1);

expands to a simple function call

Foo@@staticFn(1);

The whole point of "static" is that it is object-independent. Thus it would be impossible to virtualize.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!