tags:

views:

347

answers:

5

Let's say I have class A who inherits from class B and C (multiple inheritance). How many vtable members class A would have ? What's the case in single inheritance ?

In addition, suppose:

Class A : Public B {}

and:

B* test = new A();

Where does test gets its vtable from? What's assignment? I assume it gets B's part of A's vtable, but does A's constructor changes its fathers (B) vtable too ?

A: 

Generally speaking, a subclass will have a vtable pointer to each of the multiple superclasses it inherits from (assuming, obviously, that each of those classes have at least one virtual function).

I'm not quite sure I understood your second question. When building an object, part of the construction process is setting the relevant vtable pointers, this is something that is done implicitly by the c++ compiler by static analysis of the inheritance hierarchy. None of the vtables change, they are merely pointed at.

Yuval A
A: 

Generally speaking - you need at least one vtable entry for each virtual function you inherit. If you have no virtual functions, you have no vtable.

Tobias Langner
+2  A: 

First, vtable's are implementation specific. In fact, nowhere in the standard is specified that vtable's must exist at all.

Anyway, in most usual cases, you would get one vtable pointer per base class with virtual functions. And, as Yuval explained, nobody "fills" the vtable's when an object is constructed; you have one vtable per class with virtual functions, and objects just have pointers to their correct vtable (or vtable's, in case of multiple inheritance). In your single-inheritance example, test would have a pointer to A's vtable, assuming that A has at least one virtual function (inherited from B or newly declared in A).

Gorpik
Well, except during construction/destruction. When doing "new B", A::A if first executed with A's vtable. Then B::B is executed with B's vtable. the compiler does all the vtable pointer patching before issuing calls to the constructor methods.
Bahbar
Good point, Bahbar. I was talking about fully constructed objects, but what you say is worth remembering.
Gorpik
@Banbar: You can't say it happens this way when it may not even happen. Just like vtables how they are built is implementation dependent. As noted by Gorpik in his answer..
Martin York
@Martin. I'm specifically answering the last part ("in most usual cases"). The standard does call out that during construction, virtual calls are dispatched to the currently constructed type (12.7.4: the function called is the one defined in theconstructor or destructor’s own class or in one of its bases, but not a function overriding it in a class derived)
Bahbar
A: 

When a class defines virtual functions, the compiler silently inserts a hidden vPtr data member for each supported interface.

The vPtr points to the correct vTable for the object.

The vTable contains a list of addresses which point to function implementations.

Here's an example of sorts.

class Foo: public Bar, public Baz
{
  VTable* bar_vPtr;
  VTable* baz_vPtr;

  // Bar overrides/implementations
  void barOverride();

  // Baz overrides/implementations
  void bazOverride();
};

VTable:  
    &barOverride() // address of implementation

VTable:  
    &bazOverride() // address of implementation
Steven
A: 

When an object is created, the memory is created for the data members and not the methods. The methods will be in a common location to be accessible by all the objects. This [ one per class ] applies to VTable as it merely consists of a function pointer for each virtual function in the class.

As mentioned by Steven, compiler adds a hidden pointer to the base class, which is set when a class instance is created such that it points to the virtual table for that class, This pointer is also inherited by derived classes.

When an object of the derived class is assigned to the base class pointer, the hidden pointer in the base class is replaced with the address of the derived class's vtable.

Narendra N