Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is VTable in C++ created?

Tags:

c++

I would like to know when is a vtable created? Whether its in the startup code before main() or is it at some other point of time??

like image 492
Navaneeth Sen Avatar asked Oct 03 '10 11:10

Navaneeth Sen


People also ask

How is vtable created?

A vtable is created when a class declaration contains a virtual function. A vtable is introduced when a parent -- anywhere in the heirarchy -- has a virtual function, lets call this parent Y. Any parent of Y WILL NOT have a vtable (unless they have a virtual for some other function in their heirarchy).

When virtual table is created every class has vtable?

A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this vTable is simply a Function Pointer that points to the most-derived function accessible by that class ie the most Base Class.

Does every class have a vtable?

For every class that contains virtual functions, the compiler constructs a virtual table, a.k.a vtable. The vtable contains an entry for each virtual function accessible by the class and stores a pointer to its definition. Only the most specific function definition callable by the class is stored in the vtable.

Where is the vtable located?

Vtables themselves are generally stored in the static data segment, as they are class-specific (vs. object-specific).


2 Answers

A vtable isn't a C++ concept so if they are used and when they are created if they are used will depend on the implementation.

Typically, vtables are structures created at compile time (because they can be determined at compile time). When objects of a particular type are created at runtime they will have a vptr which will be initialized to point at a static vtable at construction time.

like image 137
CB Bailey Avatar answered Oct 05 '22 23:10

CB Bailey


The vtable is created at compile time. When a new object is created during run time, the hidden vtable pointer is set to point to the vtable.

Keep in mind, though, that you can't make reliable use if the virtual functions until the object is fully constructed. (No calling virtual functions in the constructor.)

EDIT I thought I'd address the questions in the comments.

As has been pointed out, the exact details of how the vtable is created and used is left up to the implementation. The c++ specification only provides specific behaviors that must be guaranteed, so there's plenty of wiggle room for the implementation. It doesn't have to use vtables at all (though most do). Generally, you don't need to know those details. You just need to know that when you call a virtual function, it do what you expect regardless of how it does it.

That said, I'll clarify a couple points about a typical implementation. A class with virtual functions has a hidden pointer (we'll call vptr) that points to the vtable for that class. Assume we have an employee class:

class Employee {
public:
  virtual work();
};

This class will have a vptr in it's structure, so it actually may look like this:

class Employee {
public:
  vtble *vptr;  // hidden pointer
  virtual work();
};

When we derive from this class, it will also have a vptr, and it must be in the same spot (in this case, at the beginning). That way when a function is called, regardless of the type of derived class, it always uses the vptr at the beginning to find the right vtable.

like image 43
JoshD Avatar answered Oct 05 '22 22:10

JoshD