c++: Does a vtable contains pointers to non-virtual functions?

后端 未结 4 1249
走了就别回头了
走了就别回头了 2021-02-09 03:45

vtable contains pointers to virtual functions of that class. Does it also contains pointers to non-virtual functions as well?

Thx!

4条回答
  •  梦谈多话
    2021-02-09 04:24

    Whether or not a "vtable" is used by any implementation isn't defined by the standard. Most implementations use a table of function pointers although the functions pointed to are typically not directly those being called (instead, the pointed to function may adjust the pointer before calling the actual function).

    Whether or not non-virtual functions show up in this table is also not defined by standard. After all, the standard doesn't even require the existence of a vtable. Normally, non-virtual function are not in a virtual function table since any necessary pointer adjustments and call can be resolved at compile- or link-time. I could imagine an implementation treating all functions similarly and, thus, using a pointer in the virtual function table in all cases. I wouldn't necessary be very popular. However, it might be a good way to implement C++ in an environment where it seamlessly interacts with a more flexible object system, e.g., languages where individual functions can be replaced at run-time (my understanding is that something like this is possible, e.g., in python).

提交回复
热议问题