CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Join Date
    Sep 2008
    Posts
    11

    Re: VTable questions

    As you might know V-table is a mechanism which is used to achieve polymorphism...and its implementation details shouldnt matter to someone using C++...but yeah its always good to know...its interesting....

    So its just a array(V-table) of function pointer, one v-table for each class.
    so compilers knows what all fucntions are virtual for a class, so it fills v-table with all the virtual functions.
    Now lets say you derive from a class with virtual functions, it will have its own v-table (array).... so first compiler will fill v-table of derive class with fucntion ptrs of base class and change it with virtual function implemented in derive class.....
    Every object of a class with atlest 1 virtual function will have vPtr member pointing to vtable for its class.
    If you want to see contents on v-table, you can see it in watch window (in visual studio IDE. watch for vPtr variable)

    some one asked
    "Can you acctually call the Vtable directly opposed to doing the:"
    note that its a array of function pointers...so i dont think you can use function pointer if you dont know the signature of tht function....Moreover i dont see any reason why anyone would need to call function using v-table...doesnt make sense...

  2. #17
    Join Date
    Nov 2003
    Posts
    1,405

    Re: VTable questions

    Quote Originally Posted by Peter_APIIT
    What is the approach for direct call to VTable ?
    There is no vtable in the C++ language. The vtable is a programmer's model to help explain how virtual functions work. This is very much the same way stack and heap are used.

    If you want to use a vtable, implement one in C++.

    But, of course, if you really want to write helplessly compiler dependent code, you could study the compiler documentation and code output and then access the internal data structures.
    Last edited by _uj; September 27th, 2008 at 01:49 PM.

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: VTable questions

    Quote Originally Posted by Grofit
    Sorry to ressurect this thread, after looking over what everyone has said...

    Can you acctually call the Vtable directly
    Here is a question:

    If you want to do this, why did you use virtual functions to begin with? Why not just code your own calling mechanism, instead of corrupting or finagling with the proper use of virtual functions? Maybe that is the most important question to ask your friend. The mechanism of v-tables can be simulated with proper, portable, "low-level" C++ coding, instead of fooling around with true C++ virtual functions.

    It's like buying a dishwashing machine, and asking "how do I wash dishes by hand in the dishwasher?". If you want to wash dishes by hand, don't use the dishwashing machine. If you want to call functions but not use the proper virtual mechanism, don't use virtual functions.

    Simple.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 27th, 2008 at 01:53 PM.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured