How does code find correct VTBL?

This is a discussion on How does code find correct VTBL? within the C++ Programming forums, part of the General Programming Boards category; I know there are so many questions about virtual funcion.And sorry for this question but i really don't understand how ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    How does code find correct VTBL?

    I know there are so many questions about virtual funcion.And sorry for this question but i really don't understand how code finds correct VTable(not function)

    I read here : http://www.parashift.com/c++-faq-lit....html#faq-20.4
    ıt says:"The idea is to change each Der object's v-pointer so it points at its class's v-table. (This is not a second v-pointer; it's the same v-pointer that was defined in the base class, Base; remember, the compiler does not repeat step #2 in class Der"

    I confused here.
    How many are vtables created?
    amount of base+derived class.One VTABLE for each.But all of them has one VPTR.IS this right?

    So if there is a VTABLE for each class but one VPTR; How can code find the correct function?
    I know calling virtual func. means calling an offset.But this offset is in the class's VTABLE.But first it must find one more offset that indicate correct class?
    Am i wrong?

    For example i have one base class and one virtual funct. and three derived class in a dll.Here when i call to 3. derived class virtual function from base class pointer from my exe.Here first it must find 3. VTABLE and and first offset.Right?
    But here we have only one VPTR so haw can it find 3. VTABLE and then offset of the function?

    I am looking for your answers
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    As I understand it, each compiler is free to implement it however it chooses, so long as it conforms to the rules.

    /me reads FAQ
    There you go, first statement "The answer is entirely compiler-dependent,"

    So my question is, what are you going to gain from knowing how one compiler in particular does it, when that information is pretty much useless for another compiler.

    Or put another way, is this merely idle curiosity, or are you trying to solve some problem?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23

    Exclamation

    Agreed that the compiler is free to implement as it chooses.

    I suspect the determination of the object Vtable is achieved via RTTI, but from an application perspective you should not care.

    If your motivation for asking is to solve a problem, I suggest you tread very carefully.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by SpaceCadet
    I suspect the determination of the object Vtable is achieved via RTTI,
    Unlikely. Getting the correct vtable is actually extremely easy. In the typical implementation, every constructor of a class with a vtable simply sets the vptr to its own vtable. Since base constructors are called first and derived constructors are called last, the most derived constructor is the last to set the vptr and thus "wins"; the vptr points to the most derived vtable.

    This is also one of the reasons why it's not safe to call virtual functions in constructors. If I remember correctly, the standard defines that calls to non-pure virtual functions in constructors are resolved statically, while calls to pure virtual functions are undefined behaviour.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thanks friends for answers.
    I don't want to solve special problem , i only want to understand what compiler writes to exe when virtual function callls.I think, i confused a lot so i can not define my problem well.I will give you one example then ask my question.
    Code:
    class B {
    public:
       virtual void vmf() { }
    };
    
    class D : public B {
    public:
       virtual void vmf() { }
    };
    
    //here i will write a function
    void func(B * b)
    {
       b->vmf()
    };
    Ok. Here if user enter func. parameter instance of class b then calling b->vmf() but user enter instance of class D then d->vmf() is executed.So here what does compiler writes to exe so this can be achieving.I mean what is written in func. parameter so if user enter instance of class D to func. parameter then d->vmf() is executed.Please explain this part.

    I am asking about basics of virtual function calling mechanism not special problem.

    Thanks again.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Didn't I just explain this somewhere a few days ago? I need to start keeping long posts around again.

    In short, the vtable is an array of function pointers. The call to vmf() first gets the instance-specific vtable, which depends on the runtime type of the object, of course. This is achieved by using the vptr. Then the nth function pointer in that table is looked up, where n is the index of vmf() in the table. This index is known at compile time. Finally, the call through this function pointer is performed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Is this code correct?
    By Unimatrix_001 in forum Windows Programming
    Replies: 3
    Last Post: 07-08-2003, 10:23 PM
  3. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 07:58 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21