1

I've read the following the following post Virtual dispatch implementation details and have only one thing not clear to me.

Given a polymorphic Base and a derive class - does the compiler duplicate the vftable per derive object? or all derived objects will point to the same vftable in the derived class memory space.

going into coding...

class B
{
public:
   virtual void foo() = 0;
}

class D : public B
{
   virtual void foo() {}
}

D object1;
D object2;

In the above code, will object1._vptr will point to the same address as object2._vptr ? (which means _vptr[foo position] will point to the same address in memory where foo resides)

Community
  • 1
  • 1
NirMH
  • 4,769
  • 3
  • 44
  • 69
  • 1
    All classes with at least one virtual method have a **single** vtable that is shared between all objects of the same class. – 101010 Aug 07 '14 at 11:51

1 Answers1

0

It's actually implementation dependent, but all the compilers I know of will use the same _vptr for all instances of a given class. Therefore, if you change the table entries, they will change for all instances of that class. So, yes, if you have two instances of the same class, both of their pointers to foo() will point to the same place.

Logicrat
  • 4,438
  • 16
  • 22