Is the Vtable is same for multiple instances of a particular class.

Is the Vtable is same for multiple instances of a particular class.
Suppose i am having a class

class Base
{
   public:
       virtual void Sample();
       virtual void Print();
};
class Derived : public Base
{
    public:
        void Sample();//overriding the base class virtual function.
};
void main()
{
    Base* b1 = new Derived;
    b1->Sample();//it will call the derived class function.
    Base* b2 = new Base;
    b2->Sample();//it will call the Base class function.
}

my question is Is the Vtable is same for b1 and b2.
jabaroothAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
KangaRooConnect With a Mentor Commented:
Actually the C++ standard doesn't specify how the compiler should implement virtual functions. It doesn't talk abot VTables either.
However one might assume that b1 and b2 have different VTables, since they are of different (though related) classes. One could assume that two objects of one class share the same VTable.
But that are all assumptions.
0
 
nietodCommented:
Like kangaroo said, there is no gaurantee, but usually all objects of the same class will share the same vtable.  However objects of different classes might also share the same vtable too (like if a derived class redefines no virtual functions it could use the same vtable as its base class (However typical implimentations of RTTI now make that optimization unusable)).  
0
All Courses

From novice to tech pro — start learning today.