12

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

Thx!

1
  • 5
    First, a vtable is an implementation detail. Second, why would it? You'll always call the same thing anyways. Commented Sep 30, 2012 at 12:40

4 Answers 4

13

It's an implementation detail, but no. If an implementation put pointers to non-virtual functions into a vtable it couldn't use these pointers for making function calls because it would often cause incorrect non-virtual functions to be called.

When a non-virtual function is called the implementation must use the static type of the object on which the function is being called to determine the correct function to call. A function stored in a vtable accessed by a vptr will be dependent on the dynamic type of the object, not any static type of a reference or pointer through which it is being accessed.

Sign up to request clarification or add additional context in comments.

Comments

6

No, it doesn't.

As calls to non-virtual methods can be resolved during compilation (since compiler knows the addresses of non virtual functions), the compiler generates instructions to call them 'directly' (i.e. statically).

There is no reason to go through vtable indirection mechanism for methods which are known during compiling.

Comments

2

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).

2 Comments

Are there in fact any known implementations that don't use a vtable? ( i know they don't have to, just curious)
I'm not aware of any implementation not using an embedded pointer.
2

No. A vtable only contains pointers to virtual functions in the same class or file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.