Virtual Pointer - accessing VTABLE of a class inside a program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ishwarDhumal
    New Member
    • Aug 2012
    • 6

    Virtual Pointer - accessing VTABLE of a class inside a program

    Virtual Pointer:
    Virtual pointer is implicitly added (by Compiler) to each object of a class which contains virtual function or which inherits a class containing virtual function.
    • This Virtual pointer points to VTABLE (Virtual Table) of that class. VTABLE is static member of such class.
    • VTABLE stores the addresses of functions to be invoked on polymorphic reference(At runtime).

    To access VTABLE we need its address.Compile r stores this address in Virtual pointer.This Virtual pointer is stored in first 2 bytes of an object memory(Physical ).
    So, we need an integer pointer to access first two bytes of object memory.Followin g code accesses the VTABLE of a class using integer pointer :

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    class Base
    {
    public:
    	virtual void display()  //virtual function
    	{
    		cout<<"\nBase : display()..";
    	}
    
    	virtual void goodMorning() //virtual function
    	{
    		cout<<"\nBase : Good morning..";
    	}
    };
    
    class Derived:public Base
    {
    public:
    	void display()  //virtual by default
    	{
    		cout<<"\nDerived : display()..";
    	}
    
    	//Derived does not override goodMorning()
    };
    
    int main()
    {
    	Derived d1;
    	int *vptr=(int *)&d1;  //vptr points to Derived object
    
    	//Virtual pointer is stored in first 2 bytes of object
    	vptr=(int *)*vptr; //copy virtual pointer (first 2 bytes) into vptr
    
    	//Now content of vptr is nothing but address of VTABLE
    
    	//call display()
    	((void (*)()) *vptr )();  
    	//*vptr is address of first virtual function in VTABLE viz. display()
    //and we cast VPTR to a function pointer 
    
    	++vptr;  //move 2 bytes ahead
    
    //access second function in Derived's VTABLE viz.Base::goodMorning()
    	((void (*)()) *vptr )();  
    
    	getch();
    	return 0;
    }
    Attached Files
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Why do you need to access the VTBL in the first place? There's no need to do it and if you do, you expose the implementation of the C++ language in your code making your code dependent on the implementation. This effectively, freezes your code to a specific C++ implementation.

    It is an assumption the address of the VTBL is stored in the first two bytes. The standard does not specify this since it is an implementation detail. What about 64-bit addresses?

    In your example you are incorrectly using polymorphic derived objects. You are supposed to create a derived object an access it by using a base object pointer. That is what the virtual function is all about.

    Try to write examples in C++ rather than C and without the C-style casts and macros. Use C++ cast forms.

    Comment

    • ishwarDhumal
      New Member
      • Aug 2012
      • 6

      #3
      Sir, through the above code I just wanted to test what exactly are the entries in VTABLE.In the code I casted int * to pointer to a function, using C style cast.Can you please, tell me the syntax for casting an int * to pointer to a member function using C++ style cast.I tried but not been able to do it..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I have no idea what you are trying to do.


        An int* is the address of an int. Casting does nothing but cause the compiler to use this address as something else, like a function pointer. But it still is the address of an int. In general a cast is what you use to tell the compiler to compile poorly written code.

        Please post what your intent is.

        Comment

        • ishwarDhumal
          New Member
          • Aug 2012
          • 6

          #5
          Sir, through the above code I just wanted to test what exactly are the entries in VTABLE.In the code I casted int * to pointer to a function, using C style cast.Can you please, tell me the syntax for casting an int * to pointer to a member function using C++ style cast.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The VTBL is a table of function addresses constructed by the compiler that define what function is to be called using an object that contains the address of the VTBL. The compiler manages all code involving the VTBL.

            Most textbooks, and I expect many web pages, explain this completely.

            Comment

            Working...