Click here to Skip to main content
15,893,989 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone
how many vtable the compliler will be created in multi-level inheritance
C++
class Base
 {
 public:
    virtual void function1() {cout<<"Base :: function1()\n";};
    virtual void function2() {cout<<"Base :: function2()\n";};
    virtual ~Base(){};
};

class D1: public Base
{
public:
   ~D1(){};
   virtual void function1() { cout<<"D1 :: function1()\n";};
};

class D2: public Base
{
public:
   ~D2(){};
   virtual void function2() { cout<< "D2 :: function2\n";};
};

int main()
{
  D1 *d = new D1;;
  Base *b = d;

  b->function1();
  b->function2();

  delete (b);

  return (0);
}
Posted
Updated 11-Jul-15 4:46am
v2

1 solution

hi,
basically its 'one v-table per class'. per class means, one for a class which itself has a virtual function or overides one of the parents virtual functions.

See this answer.[^]

more useful info.[^]

hope this helps !!
 
Share this answer
 
Comments
Member 11417637 9-Jul-15 7:59am    
could be explain some what more. suppose if class A have fun1()andfun2() virtual it can be overrided in class B. if some more virtual function in class B fun3 and fun4(). how many vtable in this type of inheritance
Stefan_Lang 9-Jul-15 8:24am    
Please read the solution again, including the links provided. It does fully answer your question. Also, if you follow the first link, there is a quote of the relevant definition which implies there is at most one vtable per class.
chandanadhikari 9-Jul-15 8:27am    
if class A has a virtual function then it gets a vtable . then if class B also has a virtual function then it also gets its own vtable.

Please check the second link in my answer, it has a diagram explaining all this theory.
Try to understand how polymorphism would work for virtual functions and it would become clear that why is the vtable neeed in the first place.

hope this helps!!
Member 11417637 9-Jul-15 8:39am    
Thank you
chandanadhikari 9-Jul-15 8:42am    
glad i could help and thanks for the upvote !:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900