1

vtable is an overhead in all base/derived classes when a base class has a virtual function. vtable is supposed to contain an array of function pointers to these virtual functions. Also vtable is "one per class" as opposed to "one per object".

Now imagine an object of such a class being created. It'll get a fresh copy of virtual functions of the class in some memory location by the runtime. Since vtable is a collection of function pointers it'll be updated to reflect this. If another object of the same class is created then it'll again have a fresh copy of the virtual functions in some other memory location

Since vtable is "one per class" and not "one per instance" how will it point to correct location for different instances?

3 Answers 3

8

It's simple... the vftable itself is one-per-class, the pointer is one-per-instance. Two instances of the same type will have their vftable pointer pointing to the same location.

class A
{
   virtual void foo();
   virtual void goo();
}

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

In memory, you will have:

vftable for A:  

+----------+---------+
|   0x01   |  0x02   |
+----------+---------+
  &A::foo()  &A::goo()

vftable for B:

+----------+---------+
|   0x11   |  0x12   |
+----------+---------+
  &B::foo()  &A::goo()

Assume you create two objects:

A a;
B b;

a's first member will be:

vftableptr: 0x01

b's first member will be

vftableptr: 0x11

In general, polymorphism is implemented getting the address of the vftable, adding the function offset (for example, if we call goo(), the offset is 1), and jumping to that location. Since the objects are of different types, they will point to different locations, although the vftables (which are different) can contain similar members.

IMPORTANT NOTE: values are bogus, the offset isn't 1 nor the addresses 0x01 etc., I chose them to make a point.

Sign up to request clarification or add additional context in comments.

4 Comments

if you have a single instance of a class from a class hierarchy there is no problem..what i wanted to know is that if do "new B" twice for your above code, you'll have separate copies of "foo()" in the memory, but just one vtable..virtual pointer will only point to the entries of the vtable (which is just one-per-class)..so how will the calls to virtual function of separate instances get resolved?
+1 For a good explaination. @ustulation: To expand on your problem in this context. All instances of B will have a vtable pointing to location 0x11, however the way each function of B identifies different instances of B is from the first 'invisible' argument to all member functions, which is the pointer to the instance itself, to which we refer in the function as this within the function or which is implicitly inferred when referring to member-variables or functions without using this.
oh..ok..so basically my understanding that there would be multiple copies of "foo()" wasnt correct..as Mark B seems to say also..."foo()" is always at the same place but the manipulations within it (with regards to either member variables, passed parameters or local variables) will obtain a different copy and will be resolved by "this" pointer...am i close?
@ustulation exactly... the function itself resides only in one place in memory, the vftable contains pointers to functions.
5

There aren't separate functions for every instance of a class, so the vtable always points to the same functions. Each class instances has a pointer to a vtable.

Comments

3

In implementations that use vtables, each polymorphic object has a pointer to its class vtable.

1 Comment

that's true..but each virtual pointer of an instance of a class points to same vtable for a class...pls read my comment to Luchian's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.