Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Interview: vtable for a class with a pure virtual function

I was asked this interview question today!! (it was a really awkward telephonic interview..):

What is the difference between the vtable for a class with virtual functions and a class with pure virtual functions?

Now, I know the C++ standard doesn't specify anything about vtables, or even the existence of a v-table ..however theoretically speaking what would the answer be?

I blurted out that the class with a pure virtual function could have a vtable and its vtable entry for the pure virtual function will point to the derived class implementation. Is this assumption correct? I did not get a positive answer from the interviewer.

Will a hypothetical compiler create a vtable for a class with only pure virtual functions? What if the class contains pure virtual functions with definitions? (as shown in : http://www.gotw.ca/gotw/031.htm).

like image 898
user7 Avatar asked Oct 03 '11 14:10

user7


People also ask

Are pure virtual functions in vtable?

In the case of a pure-virtual function, the entry in the vtable usually contains a pointer to a generic function that complains and aborts the program with some sensible message (pure virtual function called within this context or similar error message). A 0 indicates that this is a pure virtual function!

What is a vtable in C?

In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).

How do you declare a pure virtual function in a class?

You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration. Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A) , declaration of object a , nor the static cast of b to type A .


2 Answers

In the case of non-pure virtual functions, each entry in the vtable will refer to the final-overrider or a thunk that adapts the this pointer if needed. In the case of a pure-virtual function, the entry in the vtable usually contains a pointer to a generic function that complains and aborts the program with some sensible message (pure virtual function called within this context or similar error message).

Will a hypothetical compiler create a vtable for a class with only pure virtual functions?

Yes, it will, the difference will be in the contents stored in the table, not in the shape of the table. In a simplistic approach, a NULL pointer for pure virtual functions, non-NULL for virtual functions. Realistically, a pointer to a generic function that will complain and abort() with usual compilers.

What if the class contains pure virtual functions with definitions?

This will not affect the vtable. The vtable is only used for dynamic dispatch, and a call will never be dynamically dispatched to the definition of a pure virtual function (i.e. you can only manually dispatch to the pure virtual function by disabling dynamic dispatch qualifying the name of the type: x.base::f() will call base::f even if it is pure-virtual, but x.f() will never be dispatched to base::f if it is pure virtual.

like image 57
David Rodríguez - dribeas Avatar answered Nov 15 '22 14:11

David Rodríguez - dribeas


An implementation can do pretty much anything in such cases, because if your code ends up calling a pure virtual function in a context where dynamic resolution is required, and it would resolve to a pure virtual function, the behavior is undefined. I've seen several different solutions: the compiler inserts the address of a function which terminates with an error message (the preferred solution from a quality of implementation point of view), the compiler inserts a null pointer, or the compiler inserts the address of the function from some derived class. I've also seen cases where the compiler will insert the address of the function if you provide an implementation. The only correct answer to the question is that you can't count on any particular behavior.

like image 36
James Kanze Avatar answered Nov 15 '22 14:11

James Kanze