12.5 — The virtual table

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

Because knowing how the virtual table works is not necessary to use virtual functions, this section can be considered optional reading.

The virtual table is actually quite simple, though it’s a little complex to describe in words. First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given its own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes, which is important.

By now, you’re probably confused as to how these things all fit together, so let’s take a look at a simple example:

Because there are 3 classes here, the compiler will set up 3 virtual tables: one for Base, one for D1, and one for D2.

The compiler also adds a hidden pointer to the most base class that uses virtual functions. Although the compiler does this automatically, we’ll put it in the next example just to show where it’s added:

When a class object is created, *__vptr is set to point to the virtual table for that class. For example, when a object of type Base is created, *__vptr is set to point to the virtual table for Base. When objects of type D1 or D2 are constructed, *__vptr is set to point to the virtual table for D1 or D2 respectively.

Now, let’s talk about how these virtual tables are filled out. Because there are only two virtual functions here, each virtual table will have two entries (one for function1(), and one for function2()). Remember that when these virtual tables are filled out, each entry is filled out with the most-derived function an object of that class type can call.

Base’s virtual table is simple. An object of type Base can only access the members of Base. Base has no access to D1 or D2 functions. Consequently, the entry for function1 points to Base::function1(), and the entry for function2 points to Base::function2().

D1’s virtual table is slightly more complex. An object of type D1 can access members of both D1 and Base. However, D1 has overridden function1(), making D1::function1() more derived than Base::function1(). Consequently, the entry for function1 points to D1::function1(). D1 hasn’t overridden function2(), so the entry for function2 will point to Base::function2().

D2’s virtual table is similar to D1, except the entry for function1 points to Base::function1(), and the entry for function2 points to D2::function2().

Here’s a picture of this graphically:

Although this diagram is kind of crazy looking, it’s really quite simple: the *__vptr in each class points to the virtual table for that class. The entries in the virtual table point to the most-derived version of the function objects of that class are allowed to call.

So consider what happens when we create an object of type D1:

Because cClass is a D1 object, cClass has it’s *__vptr set to the D1 virtual table.

Now, let’s set a base pointer to D1:

Note that because pClass is a base pointer, it only points to the Base portion of cClass. However, also note that *__vptr is in the Base portion of the class, so pClass has access to this pointer. Finally, note that pClass->__vptr points to the D1 virtual table! Consequently, even though pClass is of type Base, it still has access to D1’s virtual table.

So what happens when we try to call pClass->function1()?

First, the program recognizes that function1() is a virtual function. Second, uses pClass->__vptr to get to D1’s virtual table. Third, it looks up which version of function1() to call in D1’s virtual table. This has been set to D1::function1(). Therefore, pClass->function1() resolves to D1::function1()!

Now, you might be saying, “But what if Base really pointed to a Base object instead of a D1 object. Would it still call D1::function1()?”. The answer is no.

In this case, when cClass is created, __vptr points to Base’s virtual table, not D1’s virtual table. Consequently, pClass->__vptr will also be pointing to Base’s virtual table. Base’s virtual table entry for function1() points to Base::function1(). Thus, pClass->function1() resolves to Base::function1(), which is the most-derived version of function1() that a Base object should be able to call.

By using these tables, the compiler and program are able to ensure function calls resolve to the appropriate virtual function, even if you’re only using a pointer or reference to a base class!

Calling a virtual function is slower than calling a non-virtual function for a couple of reasons: First, we have to use the *__vptr to get to the appropriate virtual table. Second, we have to index the virtual table to find the correct function to call. Only then can we call the function. As a result, we have to do 3 operations to find the function to call, as opposed to 2 operations for a normal indirect function call, or one operation for a direct function call. However, with modern computers, this added time is usually fairly insignificant.

12.6 -- Pure virtual functions, abstract base classes, and interface classes
12.4 -- Early binding and late binding

163 comments to 12.5 — The virtual table

  • Naseer

    Thank you ! This is by far the most clear explanation of vtables I have found.

  • Rag

    Good one!!!! Crisp and Clear.

  • Eli

    Thank you…this is the best explanation of vtable I have ever seen!!

  • mmk

    Very good explanation. I tried compiling above code and modified the code as below. Just changed virtual function return type else while compilation we will get error.

  • Purna Gaddam

    Vere good info. Can you provide information on how V-table is generated for Virtual Destructors.?

    Thanks in advance,

    Thanks Much,

  • Kiran

    This is the Best explanation on Vtable…..Its absolutely clear to understand.

  • Suresh

    Its really a lucid explanation. thanks a lot.

  • Surender A

    This is Really very good.

  • Pramod

    very nice information.

  • wlblmz

    good explanation, much better than wikipedia’s

  • avinash

    Very clear explanation .. thnxx …

  • alex

    Congratulations for the clear explanation!
    But I still have a doubt… I took your code and made some changes on it to show you something. Look:

    I thought this way: once D1::function2() isn’t at the VTable of D1, cause it’s not virtual, and it’s also not in the scope of Base class, the only function2() *pb can see is the Base::function2(), inherited from the own base.

    But the result of this code is this:

    How can this be possible?

    • This brings up an interesting point. Once a function is marked as virtual, it is considered virtual from that point on, regardless of whether the virtual keyword is used in derived classes or not.

      Consider the following example:

      The answer is it prints D3. Because go() was declared as virtual in Base, it’s considered virtual in Base, D1, D2, and D3. Thus pBase->go() resolves all the way up to D3::go() in this case.

      • kumar

        //good example with out key word virtual */
        class Base
        public :
        void go()
        cout<<"\n i am in base:";
        class D1 : public Base
        public :
        void go()
        cout<<"\n I am in d1:";

        class D2 : public Base
        public :
        void go()
        cout<go(); // it will execute D2
        ((D1*)ptr)->go(); // it will execute D1

        return o;

        • kumar

          • Robin

            The code that you wrote is similar to the one below 😛

    • pavan

  • aman mittal

    This learning material about virtual table is best according to me. I am searching about virtual table from a long time and i found this fulfills my requirements.

  • Lotfi

    Very simple and concise explanation of VTables. Thanks.

    What about multiple inheritance?
    And what about single inheritance but with multiple interface implementation (for the C++ CLI case)?

  • Harish

    [/code]I like this explaination, better than wikipedia[/code]

  • DSK

    Thanks for the detailed and clear explanation!!

  • It is really good explanation.Thank you

  • Really very good explanation.But still I have a doubt.The vtable is constructed at the compile time. Then how can it know the base pointer is pointed to some derived object and how can it calls the functions of that class.
    I think the virtual table contains 2 more entries reserved for RTTI(Run time type identifier) and destructor.
    I think RTTI do this operation to specify the base class pointer is pointing to some other derived class or not.

    Please give me explanation.

    • You are incorrect. The key is the following line: “When a class object is created, *__vptr is set to point to the virtual table for that class”

      The virtual tables are constructed at compile time for each class, but the *__vptr pointer is set at run time based on what type of class is actually being instantiated.

  • My concepts are not clear about the virtual tables which the compiler creates, well by reading this article, n ow it’s perfectly clear.

    Thnks a lot to the original author.

  • M V M Murali Krishna

    This is really explained very well with good exampls.

  • Prakash Agarwal

    This is by far the most clear and non-confusing explanation on V-Tables I have come through. I was searching for something like this for such a long time. Thanks to all who contributed in this. Good Job friends. Keep it up.

  • Siva Prasad. S

    This is really the perfect way of explaining V-Table mechanisam. Every C++ programmer should visit this topic.

    But, i have one doubt. As it was specified, vptr is initialized when the instance of the class is created. But, if
    the base calss is an Abstarct Class, then there is no way that an instance will be created for that class.
    So, how the vptr of an Abstract Base class is initialized?

    • Hanumanth

      Abstract class cannot create object. Creating a pointer of abstract base class can only point to derived classes. So, the _vptr can in any case will point to only derived class vtable.

  • Sumit

    Thanks so much!!! really the best explaination I came across.
    Can anyone explain to me, why a derived class pointer cannot point to a base class object. Why does it result in compiler error. The derived class object has everything the base is supposed to have?
    Thanks again!

    • Shyam

      Agreed, but if you pass this derived class pointer in any function, and this pointer access the derived member, than what would happen if you pass base class object via derived class pointer ?

    • A derived class always has all the functionality of a base class, whereas the converse is not true. So if you have a base pointer, whether you set it to a base class or a derived class, you’re guaranteed the functional in the base class is present and can be accessed through the base pointer.

      However, if you have a derived pointer, what happens when you set it to point at a base class and then try and access something that only exists in the derived class? It wouldn’t know what to do.

  • Shyam

    Very Good Explanation, this helped me alot to understand virtual table. I came across one query. Suppose I have a base class with non virtual function func1(), and virtual function func2(). I derived a class from this base class and this derived class has one non virtual function func3() and overrided virtual function func2(), than what would be the order of functions in virtual table of derived class?

  • Bala

    I was waiting for this kind of explanation such a long time. Thanks a lot ALEX

  • Dipak

    Very well explained. Thanks a lot.

  • Senthil

    Excellent explanation

  • Sidharth

    Good article….thanks a bunch.

  • sirius

    very good explanation..

  • krishna Chaitanya

    Thanks Alex for nice explanation.

    I have one doubt.
    As per your explanation “when a object of type Base
    is created, *__vptr is set to point to the virtual
    table for Base.”.

    But if I create 3 objects of type Base, as per your
    statement *__vptr is set to point to the virtual
    table for Base 3 times. But what is the need of
    setting __*vptr 3 times? Virtual table address
    should be same for all the 3 times.

    • Sumit

      “vptr” cannot be static because if it were, it would be shared by all( as you said)
      but when a base class object is made to instantiate a derived class object, the “vptr” then points to the v-table of the derived.therefore we need the vptr to point to different v-tables as per the context.

      therefore vptr is not shared by objects(static), else they would had already done that 🙂
      PS: v-tables are static for a class

  • Avnish Tyagi

    gud one!

  • Hanumanth

    Reply for “Comment by Siva Prasad. S ”
    Abstract class cannot create object. Creating a pointer of abstract base class can only point to derived classes. So, the _vptr can in any case will point to only derived class vtable.

  • kk

    ultimate explanation…good job

  • Sudipta Deb

    class Test1
    int x;
    class Test2
    virtual void mem2() { cout<<“\nTest2:mem2()”;}
    class Test3
    virtual void mem3() { cout<<“\nTest3:mem3()”;}
    class Derived:public Test1,public Test2,public Test3
    void mem4() { cout<<“\nDerived:mem4()”;}
    //int y;

    Can you please let me know the memory layout in this case?



  • Person who dont know much about c++ can also understand clearly by looking
    into this explaination. Really a good expln on vtable.


  • Arvind

    Very Nice article.
    Thanks for explaining such a difficult topic in such a simple manner.

  • good atricle…….removes sonamt confusions……..thx. man


  • Santosh


    I am very very thankful to the writer of this article. This is the best
    and nothing can be better than this.

    God bless you.


  • Anand

    Its a very good explanation I have ever seen

  • CHEN Cheng

    Hmm, IMHO, the class boxes in the above figure may not include the virtual functions, since
    the virtual functions are already put into the vtables.

    Anyway, thanks for the nice tutorial.


  • prakash.d

    This Explanation is very very much nice.And very clear.
    but i have one doubt here…
    In above explanation how it executes only one function execution explained(i.e., function1()).
    And function2() is not declared in first derived class so base class version is executed .
    My doubt is without creating object for it how can we call to the function2() of base version…?
    even a variable is declared and initialized , thats value is also appeared … as follows
    class CBase
    int a;
    CBase(){a = 5;}
    virtual void function1(){}
    virtual void function2(){cout << "this is function2() in base class and value of a is …" <function2();

    how will it executes….

  • prakashdasari

    in above doubt it is not clear to my self so again i wrote once

    really Thanks to the author for His Nice Presentation…

    here my doubt is …

    creating object for derived class pointed by the base class pointer .

    now in case of invoking function2() which is not in first derived class.
    so it calls to the base class version…
    how it does happening… with out creating object for it(base class)…

    if object is not created … then if a member varible which is already initialized
    can used in that member function (function2) to display the value…

    if object creation done …

    if we create object for a derived class …. then objects created for all base class in a chaining mechanism ..

    is it right or not

    pls give me the reason…

    i hope u r understanding my question???
    otherwise see the code here…

    class CBase
    int m_nNumber1;
    m_nNumber1 = 5;
    virtual void someMethod(){qDebug() << "somemethod in base class…" << m_nNumber1;}
    virtual void someOtherMethod(){qDebug() << "someothermethod in base class…" << m_nNumber1;}

    class CDerive1 : public CBase
    int m_nNumber11;
    m_nNumber11 = 55;
    void someMethod(){qDebug() << "somemethod in derive1 class…" << m_nNumber11;}
    void test(){qDebug() <someMethod();
    pbaseptr->someOtherMethod(); // here also the value displayed….

    Thanks in Advance
    Prakash Dasari.

  • dave

    where is the donation section….u deserve it! my prof cant do even close good explanation …

  • satya

    This is very nice and stright farward article. Thanks to author.

  • Vikas

    Simple and superb!!!!

  • Prerana

    If a base class has 1 pure virtual function then what will be the corresponding entry in the
    vtable? will it be NULL?

  • Calista

    Yes, the vtable entry will be NULL. If you think about it, a pure virtual function in a class makes it abstract which means we can never instantiate the class - which means we can’t call that function directly or indirectly from any base\derived class objects (we can’t create base class objects anyway and in case of derived class, if you define the “pure virtual function” of base class then you have provided the implementation. And if you don’t - then the derived class in turn becomes an abstract class. And so it goes on and on.

    To cut it short - yes, it will have NULL. Then you might ask who is going to invoke the NULL function? Not me. No one can call it and it also wastes memory. So Microsoft came with its own invention for that, when you declare a class with __declspec(novtable) for abstract base classes - you are effectively saying - “I know what I am doing. I don’t want to declare the vtable for this abstract base class, since no one can instantiate an object of this class anyway. So please save some memory and donot construct the vtable. Thank you”.

    Also consider C# - to declare a class as abstract all you have to do is:
    “abstact class IAmIntuitiveAbstractClassDeclaration”

    Isn’t that intuitive instead of define virtual function, and then make that virtual function “pure” - = 0? I might have as well said “you can declare an abstract base class by making one of the virutal functions a zombie and declare it like this

    “virtual void laspseInDesign const = Zombie” Seems ad hoc to me.

  • Puneet

  • jobin

    D1 cClass;

    Base *pClass = &cClass;


    How pClass gets the *__VPTR pointer of D1.

    • Please careful experience the following words.

      Because cClass is a D1 object, cClass has it’s *__vptr set to the D1 virtual table.
      And then you set a base pointer to D1.
      Because pClass is a base pointer, it only points to the Base portion of cClass. However, also note that *__vptr is in the Base portion of the class. so pClass has access to this pointer. Finally, note that pClass->__vptr points to the D1 virtual table!

  • jobin

    Hi alex,

    Could you please give your help to solve the above query?

  • manu

    Thank you very much.very nice article.

  • manu

    can you please post some articles on function overloading like this.

  • Venkat

    Very nice explanation on Vtable..ThankQ
    but i have one basic doubt.
    we created appropriate object and compiled.
    Vtable and object creation(so *vptr is initialized) everything is done during compile time itself.and we clearly mentioned base pointer is pointed to derived obj.So what is the special thing that will happen during run time and why it called late binding.

    • Paras Jatkar

      Object allocation in the memory will be done at run time and assignment of that allocated memory address can be assigned only at Run time.
      This will happen at the Run time and hence called ‘Late binding’.

      Its a nice Explanation from the original author..
      Good Job! 🙂

  • Vijay Chulaki

    ALEX, Thanks a lot…for helping many of us to understand ‘vtables’.

    Q: can any one let me know if we can display the ‘vtable’ for a class? If yes, HOW?

    ref : ‘’. Here they are displaying the vtable for a class as below :

    G++ 3.4.6 from GCC produces the following 32 bit memory layout for the object b2:[nb 1]

    +0: pointer to virtual method table of B2
    +4: value of int_in_b2

    virtual method table of B2:
    +0: B2::f2()
    Vijay Chulaki

  • Ishdeep

    Great explanation. Many thanks.

  • Anil

    Its really good and simple to understand about Virtual function and VTable. Can you please explain the second step in your explanation [Index the virtual table to find the correct function to call].
    Thank you.

  • som

    Well i know i can point a derived class object to a base class, but not vice versa.

    But is it possible to create the derived class objects through base class object.

    Well i have class Base, form which class D1, D2, and cD3 are derived.

    Now using some condition, i want to create objects of D1, D2 and D3 with the object of the base.

  • kumar

    • Roger Gonsalves

      Here you are creating object of D2 and storing it in base class pointer ptr.
      Since there is no relation between D1 and D2, typecasting of ptr to D1 will lead to heap corruption.
      I came across this type of issue in my code.

  • satya

    Thanks a Lot.
    This is by far the most clear explanation of V-table i ever read.


  • consider array of derived class object {d[0],d[1],d[2],d[4],d[5]};
    and base class pointer points the first element of the array.
    what is the output when increment the base class pointer. where it is point?


  • vikas kumar gupta

  • Sadiq

    It’s beyond being helpful, indeed. 🙂

  • Uwe Schuster

    This makes the question: In which compilation unit the vtable of a class is placed by the compiler. For instance, if we have the case:

    most compilers will do somewhat like this: The vtable is placed in the object file with the definition of the first non-pure, non-inline, virtual function of the class (classAf.o in this example) with external linkage.

    But what if we have:

    useA1() and useA2() must have access to the vtable of class A, but where is it defined, since the compiler rule from previous example does not match here?

    In can be defined in both useA1.cpp and useA2.cpp, but than it must have static linkage.

    Right or do I miss something?

  • HC Reddy

    Thanks a lot…it is crisp, simple and straight

  • lalit

    ohhhh… its really too good. explanation thank u very much…

  • priyanka

    Hey really good explanation!!!Thanks a lot!!

  • itnovice

  • Vijay

    Wonderful explaination. Can you explain diamond (virtual) inheritance as well ?

  • Giri

    This diagram is all what’s needed to explain the whole story. Thank you very much.

  • Abhinav Raghunandan

    Truly awesome !!!! The most simple and precise explanation .

    -Student of IIT Madras

  • MBM

    Hi Alex,
    It is a very nice explanation.
    I have one query: If *__vptr value is same for all instances of a class. why it can not be “static” (initialized only once per class) and why it needs to be replicated in all instances (which adds additional pointer size to all instances).

    • Soumya


      it can’t be static because it will change the address according to the allocation.

      C1 *cClass= new C1();
      D1 *dClass= new D1();
      Base *pClass = &dClass;
      Base *p1Class= &cClass;

      vptr of pClass instance is pointing D1 vtable
      vptr of p1Class instance is pointing C1 vtable

  • Ravikumar

    Heading : Non virtual function of base class is getting executed though it is overridden
    in the derived class.

    Please find the code sample below.

    class vehicle
    virtual void speed()
    cout << "In base speed" << "\n";

    virtual void maintain()
    cout << "In base maintain" << "\n";

    void value()
    cout << "In base value" << "\n";


    class wheel4 : public vehicle
    virtual void speed()
    cout << "In 4wheel speed" << "\n";

    virtual void maintain()
    cout << "In 4wheel maintain" << "\n";

    void value()
    cout << "In wheel4 value" <value();
    After the execution of ptr->value(), the function present in the class “vehicle” is
    getting executed. As per my understanding the “value” present in wheel4 should
    be executed.

    What is the reason for this?

    • Soumya

      Non virtual function can’t be overridden in derived class. only speed and manitain method is overridden in wheel4 class.

      for that reason it is printing vehicle’s class value method output.

    • Deepu Abraham K

      As Alex had stated in the above diagram,

      When you do
      vehicle* vobj = new wheel4;
      here what happens is *ONLY* the *__vptr of vehicle class will get updated with the vtable of wheel4.

      remember that you have only one function in the virtual table i.e, void speed()

      if you want to print “In wheel4 value” make the void value() function in the vehicle class as virtual void value() by doing this you are adding this function into the virtual table..

      the crux of this revolves around the *__vptr getting filled by the vtable pointer.This is how c++ obtains the dynamism which it claims.

      Hope this clear the air !

  • Soumya

    First i want to give thanks for a very good explanation. But i have some queries related with it for that reason i have written a demo program. the program is as below.


    class D1{

    void b1(){cout<<"D1:b1";}
    void b2(){cout<<"D1:b2";}
    virtual ~D1(){cout<<"D1";}

    class B1: public D1{

    virtual void b1(){cout<<"B1:b1";}
    void b2(){cout<<"B1:b2";}

    class B2:public B1{
    void b1(){cout<<"B2:b1";}
    void b3(){cout<<"B2:b3";}

    class C1: public B2{

    void b3(){cout<<"C1:b3";}
    delete b;

    B1 *b= new C1();
    delete b;

    return 0;


    Now in this case i think every time runtime reindexing will happen on the vitual table of C1 class before pointing base class _vptr to point to the C1 class vtable so that it only access the correct method list so first reindexing then pointing. am i right?

    Then my next point is after reindexing base class will be able to access the destructor of the derived class if we declare the destructor as virtual in base class. As we know the signature of the destructor is varying form base to derived class then through which mechanism it is resolving the destructor signature issue.

    • Soumya

      Hi my last portion of code is not properly pasted so i am providing it further.
      class C1: public B2{

      void b1(){cout<<"C1:b1";}
      void b2(){cout<<"C1:b2";}
      void b3(){cout<<"C1:b3";}
      delete b;

      B1 *b1= new C1();
      delete b1;

      return 0;


      • Soumya

        i have another question suppose i have two functions func1 is virtual and func2 is non virtual in class B1.
        now if we write

        B1 *b=NULL;

        and try to access the functions. we will be able to access the non virtual function but we will not be able to access the virtual.

        What is the reason of it? why we are able to access the non virtual functions and why we are not able to access the virtual functions?

  • cpp-learner

    Thank you, thank you, thank you. Absolutely the best tutorial written for anything in human history.

  • Thanks! Best explanation on v-table I have come across. May be make this as wikipedia entry and replace the existing one! This is far better than that!

  • ravindra

    This is a very good explanation of vtable concept in c++.I got the concept at the first glance itself.Great work .
    keep going

  • great man. i have not such an easy explanation of vtable

  • Ramya

    Superb Explanation by original author. I am very impressed.

  • Thanks for good explanation.. but i could not understand about the function calling from vptr. how is it
    determine which function need to call. li

    “it looks up which version of function1() to call in D1’s virtual table. This has been set to D1::function1(). Therefore, pClass->function1() resolves to D1::function1()!”

  • very good explanation in a simple way for the c++ beginners..

  • mrityunjay

    Very good explanation

  • Sankar

    Thanks for post…

  • Kollol

    Very neat explanation. The best I’ve ever come across on V-Table. Cheers Alex!

  • Marvellous!! I think i have got a perfect answer for my long lasting question.
    The way each and every step has been explained is crystal clear. Moreover,
    I have been through almost all the comments and found them interersting which faded away
    my ambiguity regarding this topic.

  • Hello,

    with your example:

    when doing a copy by value :

    the displayed result is “Base::function1” .
    I thought it should be “D1::function1” as *__vptr points to D1 virtual table.

    So why it calls Base::function1 ?


  • Very clear and lucid explanation..
    Even the comments are good.
    Thanks for sharing…

  • saini

  • saini

  • Roger Gonsalves

    Above code generates following output.

    Inside ABC::Draw()
    Inside DEF::Draw()
    Inside DEF::Draw()
    Inside ABC::Draw()

    Can anyone explain how vtable are created and accessed in above code?

  • Check this link.

    It give more info on vtables.

  • David


    pClass is initialized with Base’s vtable.
    Virtual Table’s are not copied.

    would however invoke D1:function1 as pClass refers/points to cClass which had its *__vptr to be initialized to D1’s vtable.

  • anupama

    i wanted to know the order of functions how it is stored in the vtable.
    class Base
    virtual void function1() {};
    virtual void function2() {};

    class D1: public Base
    virtual void function3() {};
    virtual void function4() {};

    how will the vtable for D1 look like?
    is it - function1()


    Thanks in advance

  • VishalRastogi

    Really nice article to clarify virtual table concepts..

  • Sanjeev

    One of the best article I have come across on vTable concepts. Thanks buddy!!!

  • devyani

    great one…!!!! helped me a lot! thanks! 🙂

  • brahmaji_m

    Wonderful ! Finally found a simple explanation for Vtables

  • chezhian.p

    very useful and made the concept very understandable.

  • sapnads


    That was a really good article. However I have a question. What if objects of both base classes are created before you use a pointer of type BaseClass. Eg:

    D1 c1Class;
    D2 c2Class;
    Base *pClass = &c1Class;

    what does the hidden pointer point to now?

    Thanks. I really appreciate the help.

  • ajloeffl


    All you are doing is making a base pointer (pClass) that points to an already created derived class (D1). This is standard practice and the *__vpr points to the virtual function table for a D1 object type.

  • Dolly

    Excellent article For Virtual Table.

  • Chetan

    Muhaa thank you soo much for posting this about Vtable .

  • akjace

    very well written!

  • partha

    strong technique explained with simplicity. good work. thanks much !!

  • Punit Mishra

    Excellent article on VTable.

  • Kratos

    Really good and simple to understand article on the intricacies of a vtable that mostly create a lot of confusion. Excellent job!!!

  • anantharaj

    Really nice explnation.

  • ss005

    I am with all other guys, A NICE, GREAT AND BEAUTIFULL EXPLANATION….

  • Susil Kumar Rout

    It’s a very good article.Everyone can understand vtable easily from this article

  • tomriddle

    I have a question for this example.
    class A
    virtual void Function1();
    virtual void Function2()=0;
    class B : public A
    void Function1();
    void Function2();
    class C : public B
    void Function1();
    void Funtion2();
    1.In this case,when i create an object of type C, where the __vptr will point to?
    2.Is there any different with the pure function when a vtable is created in class A?

  • sumit agrawal

    simply aweesummm…..

  • ab

    simple and easily understanding post

  • Jason Li

    Question, any help from any one is appreciated!

    Suppose a base pointer is pointing to an object of D1, and now I want to override the virtualization via the base pointer (covered in 12.3). That is, I want to use Base::function1() instead of D1::function1(). The question is, how does the virtual table schematic work out this case?

    According to the tutorial, the Base::function1() is replaced by D1::function1() in D1’s virtual table, so it has already lost track of the original Base::function1(), so how can it recall where the Base::function1() is?

  • abhi4556

    How the above concept will work on multiple inheritance? Please help me.

  • DeepakR

    explain me what will be the Vtable entry and how it will execute the below code as per ur this article:

    class A
    virtual void show()
    cout<<" i am in A";


    class B : public A
    void show()
    cout<<" i am in B";


    class C : public B
    void show()


    return 0;

  • Gane

    Well, Nice info about the VTABLE appreciated. Thanks, Please keep this going…

    Well documented with examples and illustrations.

  • atuldabral

    Thanks . First time i got to know the how vp works.

  • Mukesh Modi

    Only one time to read is sufficient…. no more to read other article….awesome article..thanks

  • Sind

    Great article. Very very clear.

  • Basim Alamuddin

    A clear and easy explanation, thanks.

  • patrick

    Very clear and concise explanation

  • puppi

    how can i get these virtual function’s point addresses? is there any way to get it?

  • AbdullahEsam

    can you please explain what this sentence means: "most-derived function"

  • ranjeet


    How we can see the *_vptr and Vtable in linux using g++ compiler,

    Please explain ?


  • kot

    Great explanation! Thank you.

  • Ganesh Salvi

    This is BEST explanation I found. Bookmarked this link!!! Super job and thank you!

  • sagar

    good job..!!! You are doing some really great work.Keep it up bro…!!!!God will help you out..!!!

  • Awesome article..only one time to read is sufficient….no more to read other article..thanks

  • Dan

    See all those good comments, but nobody asked how this was true? At least was not explained.

    Note that because pClass is a base pointer, it only points to the Base portion of cClass. However, also note that *__vptr is in the Base portion of the class, so pClass has access to this pointer. "
    "Finally, note that pClass->__vptr points to the D1 virtual table! Consequently, even though pClass is of type Base, it still has access to D1’s virtual table"

    I thought you just mentioned pClass only can access *__vptr is in the Base portion of the class

    • Alex

      Because pClass is a Base, it can only directly access Base members (this includes __vptr). However, because pClass->__vptr points to D1, pClass can access the D1 virtual table through __vptr.

      • Vineet

        Hi Alex,

        I have added one new virtual function

        in D1. So D1 virtual table will have this new function. As per your explanation I should be able to access to function3() with pClass->__vptr. But that fails. I get compile error. What is explanation for that?

        • Alex

          You shouldn’t try to access the virtual table directly (nor should you ever need to). pClass->function3() should work fine. If you’re getting an error, I’ll need more code and the specific error you’re getting to debug further.

          • Vineet

            code is:


            • Alex

              Ah! I see what you’re getting at. Given the above example, pClass->__vptr will point at D1’s virtual table, which has Function3() in it (pointing to D1::Function3()). So in this particular case, this could resolve to a valid function call at runtime.

              But as you’ve noted, the compiler won’t even let you compile it (since Function3() isn’t accessible through Base). Just because something is in the virtual table doesn’t mean the compiler will let you use it.

              Remember, the virtual table is a structure used at runtime to resolve function calls. But it doesn’t control access -- the compiler handles whether you should or should not be able to call a function.

  • Sudhakar

    Good Explanation. great work Alex

  • Matthieu B.

    There’s a typo that has been waiting for more than 8 years in "First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table.". "it’s" should be "its".

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter