Explanation of Vptr and V-table

Discussion in 'C++' started by pai, Oct 18, 2006.

  1. pai

    pai Guest

    Hi,

    Can any body explain me about V-table and Vptr ( virtual pointer ).
    ANy link is also welcomed.
    I didnt get useful explation about the topic .I mean some what detail
    explation .
    As what happens when i add virtual keyword to a function and so is
    done by the compiler at that time and also if I inherit the base class
    which has a virtual function will it create one more v-Table and Vptr.

    Thank You

    Regards
    Pai
    pai, Oct 18, 2006
    #1
    1. Advertising

  2. pai

    Ron Natalie Guest

    pai wrote:
    > Hi,
    >
    > Can any body explain me about V-table and Vptr ( virtual pointer ).


    They are internal guts to the way compilers MIGHT implmeent virtual
    functions and RTTI (dynamic_cast, typeinfo). By and large you need
    be unconcerned with them. They vary from compiler to compiler and
    even depending on the compiler switches.

    > As what happens when i add virtual keyword to a function and so is
    > done by the compiler at that time and also if I inherit the base class
    > which has a virtual function will it create one more v-Table and Vptr.
    >


    Again this is implementation specific, but the truth is that as
    soon as you have a polymorphic class (any virtual functions in it),
    there is a slight overhead created (which is why the language
    distinguishes between polymorphic class and not). Similarly
    a virtual function may have small performance overheads on it's
    invocation when compared to a non-virtual function.

    If you inherit from a polymorphic class, even if the new class
    doesn't declare any virtual functions, it is also polymorphic.

    The above is the official language guidance.

    Now, that being said, most compilers do use a vtable to implement
    these concepts. A vtable is a list of pointers to virtual
    function implementations for each virtual function defined (or
    inheritted within) a function as well as a pointer (or the
    actual data) for the typeinfo values. Only one vtable is
    created per class defined (not per object). Each object
    gets a pointer to the vtable so each object of a polymorphic
    class gets larger by one pointer (the vptr) that points to
    it's vtable.

    That's fairly reliable. The layout of the vtables and how
    the pointers got to get bashed around plus the vagaries of
    how you have to bash pointers to members to get everything
    to work are highly implementation specific.
    Ron Natalie, Oct 18, 2006
    #2
    1. Advertising

  3. pai

    Daniel T. Guest

    "pai" <> wrote:

    > Can any body explain me about V-table and Vptr ( virtual pointer ).
    > ANy link is also welcomed. I didnt get useful explation about the
    > topic .I mean some what detail explation . As what happens when i
    > add virtual keyword to a function and so is done by the compiler at
    > that time and also if I inherit the base class which has a virtual
    > function will it create one more v-Table and Vptr.


    It's magic. The standard requires no v-table or virtual pointer; if one
    happens to exist, it is of no concern to you or your code. But if you
    really need to know, you can consult your compiler documentation about
    how it handles virtual function dispatch.

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Oct 18, 2006
    #3
  4. pai

    mlimber Guest

    pai wrote:
    > Can any body explain me about V-table and Vptr ( virtual pointer ).
    > ANy link is also welcomed.
    > I didnt get useful explation about the topic .I mean some what detail
    > explation .
    > As what happens when i add virtual keyword to a function and so is
    > done by the compiler at that time and also if I inherit the base class
    > which has a virtual function will it create one more v-Table and Vptr.


    See this FAQ:

    http://www.parashift.com/c -faq-lite/virtual-functions.html#faq-20.4

    Cheers! --M
    mlimber, Oct 18, 2006
    #4
  5. pai

    Amol Guest

    Hi, Pai

    The best explanation about vtbl and vptr I have seen is in the Bruce
    Eckel's "Thinking in C++", 2nd Ed. Vol. 2. Which is also available in
    pdf and HTML formats for free on the authors personal website
    www.BruceEckel.com.

    Eckel devoted 4-5 pages to explain vtbl and vptr.
    Amol, Oct 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Veeru
    Replies:
    7
    Views:
    1,217
    tragomaskhalos
    Sep 15, 2006
  2. Subhransu Sahoo

    A basic doubt on VPTR and VTABLES

    Subhransu Sahoo, Nov 7, 2006, in forum: C++
    Replies:
    4
    Views:
    336
  3. Rahul

    vptr

    Rahul, Nov 4, 2007, in forum: C++
    Replies:
    3
    Views:
    638
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Nov 4, 2007
  4. Thomas Lenz

    vtbl and vptr

    Thomas Lenz, Dec 16, 2008, in forum: C++
    Replies:
    18
    Views:
    1,679
    Laurent Deniau
    Dec 17, 2008
  5. VPTR and VTABLE

    , Dec 1, 2009, in forum: C++
    Replies:
    9
    Views:
    4,098
    James Kanze
    Dec 2, 2009
Loading...

Share This Page