Link to home
Start Free TrialLog in
Avatar of jabarooth
jabarooth

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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)).