Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • VTABLE and VPTR in C++

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.33k
    Comment on it

    VTABLE-  C++ uses a special form of a runtime method  binding to implement virtual functions which is called virtual table. Virtual table is a table of functions used to accomplish function calls in a dynamic binding manner. Virtual binding is also known as vtable, virtual function table, virtual method table or dispatch table.

    Every class uses a virtual function whether it is a class which uses virtual function or the class which is derived from that class and having own virtual table. Each entry in vtable is a function pointer that is called by objects of that class.

    e.g of VTABLE-

     

    class Base
    
    {
    
    public:
    
        virtual void function1() {};
    
        virtual void function2() {};
    
    };
    
    
    class D1: public Base
    
    {
    
    public:
    
        virtual void function1() {};
    
    };
    
    
    class D2: public Base
    
    {
    
    public:
    
        virtual void function2() {};
    
    };

     

     

    VPTR-  VPTR is known as virtual table pointer. Compiler added a hidden pointer to the base class known as VPTR which is pointing to the table of the function pointer of that class. Every class which is derived by the base class(derived class) can inherit this.

    e.g of VPTR-

    c

    class base
     {   virtual void funct1(void);
     virtual void funct2(void);
    }; base b;
    
    b.vptr = address of b.vtable
    b.vtable[0]= &base::funct1()
    b.vtable[1]= &base::funct2()


     

     

     

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: