tags:

views:

104

answers:

2

Hi,

Can some one explain me how inheritance is implemented in C++ ?

Does the base class gets actually copied to that location or just refers to that location ?

What happens if a function in base class is overridden in derived class ? Does it replace it with the new function or copies it in other location in derived class memory ?

+2  A: 

Inheritance in C++ is often accomplished via the vtable. The linked Wikipedia article is a good starting point for your questions. If I went into more detail in this answer, it would essentially be a regurgitation of it.

SB
@sb a vtable is not strictly required for inheritance. See the answer of Ichthyo.
David Feurle
@SB there is no need for vtable for inheritence. Its required for dynamic binding right ?
brett
for dynamic binding, yes. In case a vtable gets used for a given member function, the "iheritance" handling is done at initialisation of the vtable, by placing the pointer to the right function into the vtable. Typically, a C++ compiler generates a (hidden) function for each distinct type, which, when called, emits and configures the vtable for this type. (All of this can be more involved when multiple inheritance comes into play, but the principle remains the same)
Ichthyo
@Ichthyo: Compilers will not usually update pointers *at initialization* of the vtable. In most cases, given a class `base` with at least a virtual method and a derived class `derived` (for simplicity consider that `derived` overrides, but does not add more virtual methods), the compiler will emit two different fixed vtable's, one for `base` and one for `derived`. During object initialization, after the `base` constructor has completed and before entering the rest of the derived initialization list, the virtual table pointer is updated --not the pointers in the vtable, but a pointer to vtable.
David Rodríguez - dribeas
another aspect worth mentioning is that due to this initial setup of the vtable(s), creating *lots* of different types with virtual functions can significantly bloat your executable. (you can get lots of different types by careless use of templates)
Ichthyo
+4  A: 

Hi brett,

first of all you need to understand that C++ is quite different to e.g. Java, because there is no notion of a "Class" retained at runtime. All OO-features are compiled down to things which could also be achieved by plain C or assembler.

Having said this, what acutally happens is that the compiler generates kind-of a struct, whenever you use your class definition. And when you invoke a "method" on your object, actually the compiler just encodes a call to a function which resides somewhere in the generated executable.

Now, if your class inherits from another class, the compiler somehow includes the fields of the baseclass in the struct he uses for the derived class. E.g. it could place these fields at the front and place the fields corresponding to the derived class after that. Please note: you must not make any assumptions regarding the concrete memory layout the C++ compiler uses. If you do so, you're basically on your own and loose any portability.

How is the inheritance implemented? well, it depends!

  • if you use a normal function, then the compiler will use the concrete type he's figured out and just encode a jump to the right function.
  • if you use a virtual function, the compiler will generate a vtable and generate code to look up a function pointer from that vtable, depending on the run time type of the object

This distinction is very important in practice. Note, it is not true that inheritance is allways implemented through a vtable in C++ (this is a common gotcha). Only if you mark a certain member function as virtual (or have done so for the same member function in a baseclass), then you'll get a call which is directed at runtime to the right function. Because of this, a virtual function call is much slower than a non-virtual call (might be several hundered times)

Ichthyo
I agree that normal functions can be used, but if not using virtual, don't you have to cast the object to the concrete type before making the method call? http://en.wikipedia.org/wiki/Virtual_function
SB
A virtual method call can be slower than a non-virtual method call, but from there to *much slower* and *several hundred times* slower I think you are over exaggerating **a lot** (and I know that *over exaggerate* is an improper exaggeration in itself). Without multiple inheritance, a virtual method call will only take an extra level of indirection from a direct method call. While that can slow down things, in particular when that impacts branch prediction in the processor, I would find it hard to consider that to cause a 1:100 performance degradation per-se.
David Rodríguez - dribeas
... also note that the compiler does not need to use the virtual dispatch mechanism it it can resolve at compile time the actual type of the object --as would be required for non-virtual methods.
David Rodríguez - dribeas
David Rodríguez - dribeas
@dribeas agreed that the difference isn't so large on average. But I want to stress the fact *that* there is a difference, which seems to be difficult to grasp for developers e.g. grown up e.g. with java or C#.
Ichthyo
ah and to complete the picture, we should mention the possibility of *inlining*: If the compiler has a full *definition* of the member function to be called available at the call site, then he might decide not to do any function call and rather copy the function code in place and adjust it accordingly. As each function call requires the compiler to generate some "boilerplate" code for argument passing and return value etc, for very short functions this can give an dramatic performance boost. (but as with any optimisation, your milage may vary,you'd need to measure in each practical situation)
Ichthyo
...obviously only a statically dispatched function call can be inlined
Ichthyo
@Ichthyo: But even calls to virtual functions can sometimes be dispatched statically.
Ben Voigt