17

What is virtual pointer? Hi, ALL, Today I had an telephone interview and got a question: "What is virtual pointer?" I stumbled on this one, so after it was finished, I tried Google. Unfortunately, it gave me a virtual table references only.

So what is virtual pointer in plain English? How do you define it?

Thank you.

Tim
  • 20,184
  • 24
  • 117
  • 214
Atul
  • 1,157
  • 2
  • 16
  • 28
  • 5
    As far as I am aware, there's no such thing, unless your interviewer was talking about a single entry in the vtbl structure. – Billy ONeal Jan 11 '11 at 14:29
  • 2
    Virtual *table* pointer? – Nikolai Fetissov Jan 11 '11 at 14:33
  • It was an advanced interview question to probe how well you know the C++ object model implementation details. – Maxim Egorushkin Jan 11 '11 at 14:35
  • I knew about vtable ptr but not virtual pointer . what should I have replied ? – Atul Jan 11 '11 at 14:41
  • @Atul, you should have said, "there is no such thing", in this particular case I wouldn't worry about it, if they reject you on the performance on one question (and not a very good question at that), I'd look else where.. – Nim Jan 11 '11 at 15:02
  • 4
    The Occam's Razor offers the most likely explanation: the interviewer was less competent than interviewee, because the latter at least suspected that there is no such thing. – Gene Bushuyev Jan 11 '11 at 15:03
  • The compiler source, I think contains "vptr" as variable name. I don't remember which one out of clanf/gnu etc – Vishesh Mangla Jul 04 '23 at 05:07

5 Answers5

17

There is no such thing as a "virtual pointer."

There are a few things the interviewer might have meant:

  • A pointer to a polymorphic class
  • A pointer to the vtable of a polymorphic class (credit @Maxim)
  • A pointer within the vtable of a polymorphic class
  • A smart pointer object with an overridden operator->
  • A pointer to a virtual member function (credit @ Matthieu M)

But as far as "virtual pointer" is concerned, there's no such thing.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • @John: I would add, pointer to virtual member function to the list of likely possibilities. – Matthieu M. Jan 11 '11 at 15:16
  • You would most likely fail the interview using one of the above answers. – Maxim Egorushkin Jan 11 '11 at 15:33
  • @Maxim: Fine with me, the interviewer is a moron. Now, is there something specific that is factually incorrect about my response, or are you just trying to get back at me for downvoting you a few days ago? – John Dibling Jan 11 '11 at 15:38
  • @Maxim: Incedentally, one of my bullet points is the same as your answer. Would you have failed too? – John Dibling Jan 11 '11 at 15:39
  • @John: I knew you would mention that down-voting, lol. Peace man. IMNSHO, virtual pointer could only mean a pointer to the virtual table. You don't have that answer in your list. – Maxim Egorushkin Jan 11 '11 at 15:42
  • @John: vtable is an array of pointers to virtual functions of a class, so "pointer within the vtable" is an element of that array. Whereas vtable pointer is the pointer to the vtable. – Maxim Egorushkin Jan 11 '11 at 15:47
  • @Maxim: Fair enough. Edited. – John Dibling Jan 11 '11 at 15:48
  • +1 for a good list. There's a lot of things it might be. I'd personally guess at 'pointer for which the static type and dynamic type are different'. – JoeG Jan 11 '11 at 15:49
  • @Joe: Not sure what you mean. Do you mean a base-class pointer to a derived-class object? – John Dibling Jan 11 '11 at 15:50
  • @John: I would suspect he does, classes are the only thing with a "dynamic type". – Matthieu M. Jan 12 '11 at 07:15
  • for an explanation of what is a "A pointer to the vtable of a polymorphic class", see above the answer of @Ather Parvez. –  Jan 21 '21 at 16:29
10

C++ compiler creates a hidden class member called virtual-pointer or in short vptr when there are one or more virtual functions. This vptr is a pointer that points to a table of function pointers. This table is also created by compiler and called virtual function table or vtable. Each row of the vtable is a function pointer pointing to a corresponding virtual function.

To accomplish late binding, the compiler creates this vtable table for each class that contains virtual functions and for the class derived from it. The compiler places the addresses of the virtual functions for that particular class in ‘vtable’.

When virtual function call is made through a base-class pointer, the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the right function and causing late/dynamic binding to take place.

Ather Parvez
  • 101
  • 1
  • 2
  • Thank you, this answer is really clear. For french developers, this is a little bit explained in the chapter 1 of the book : "Pour mieux développer avec C++ : Design patterns, STL, RTTI et smart pointers", Aurélien Géron, Fatmé Tawbi. –  Jan 21 '21 at 16:26
6

Your interviewer most likely meant virtual table pointer. http://en.wikipedia.org/wiki/Virtual_table#Implementation

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
5

My interpretation would be: the contents of a vtable—pointers to virtual methods.

Not a very good wording, IMHO.

JB.
  • 40,344
  • 12
  • 79
  • 106
2

It could also means create a function pointer of virtual / virtual pure of a father's method and call it with a child, still it's not a good wording ...

Errata
  • 640
  • 6
  • 10