Virtual Pointer

前端 未结 5 1893
迷失自我
迷失自我 2021-02-07 16:04

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 G

相关标签:
5条回答
  • 2021-02-07 16:18

    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.

    0 讨论(0)
  • 2021-02-07 16:19

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

    0 讨论(0)
  • 2021-02-07 16:20

    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.

    0 讨论(0)
  • My interpretation would be: the contents of a vtable—pointers to virtual methods.

    Not a very good wording, IMHO.

    0 讨论(0)
  • 2021-02-07 16:37

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

    0 讨论(0)
提交回复
热议问题