POV-Ray : Newsgroups : povray.off-topic : Lots of statistics : Re: C# Server Time
29 Jul 2024 10:31:48 EDT (-0400)
  Re: C#  
From: Warp
Date: 14 Aug 2012 07:11:34
Message: <502a3266@news.povray.org>
Le_Forgeron <jgr### [at] freefr> wrote:
> >> I have to admit, Eiffel /does/ make the solution look very, very 
> >> complicated. (I can't actually remember off-hand how the heck C++ does 
> >> it...)
> > 
> > Does what?

> Manage inheritances, including multiple inheritances.

In a single inheritance case the compiler simply internally builds a
data structure with all the data members of the entire inheritance
hierarchy one after another. The base class portion of the structure
looks identical to the one that would exist if you instantiated the
base class directly (and hence any function that deals with the base
class members can deal directly with the inherited class object).

If the class contains any virtual functions, then a virtual table pointer
will be added to the data structure (typically, though not necessarily,
at the beginning of it).

(If the base class did not contain any virtual functions but the derived
function does, it's perfectly possible for the 'this' pointer to change
in value when a base class method is called, in order to point to the
base class part of the structure. The base class member function will be
none the wiser, and work just right.)

In a (non-diamond) multiple inheritance case the same thing is done,
basically. The data of the base classes is just put one after the other
in the derived class' data structure (and the 'this' pointer is changed
as necessary when calling the base class member functions).

In a diamond inheritance situation you have a choice to make: Do you want
each of the multiple parents to be "independent" of each other (which
means that the data from the common parent class is kept separately),
or do you want them to share the data from the common parent class?

In the latter case the data from the common parent class is stored only
once in the most-derived class' data structure, and virtual table trickery
is used to make the in-between class methods to change the 'this' pointer
appropriately when calling the base class methods (or if the in-between
classes want to read/change the base class member variables directly).

(In the former case the in-between classes behave just like there was
no multiple inheritance at all, handling their own "local copy" of the
common base class.)

> >> I would have thought the indirect code jump is probably far more 
> >> expensive than the space overhead.
> > 
> > The additional space consumption is significant if you have eg. a class
> > that would otherwise be the size of a pointer (and you need to instantiate
> > millions of them). If you had forced virtual functions, it would double in
> > size.

> IIRC, C++ does not duplicates the class function members (not even
> pointers to them) in each instance. Only the actual class (as 1 pointer)
> is stored within each instance. The virtual function is just an index.
> The first virtual/inheritance cost a pointer in the class. The next are
> free.(in fact, with RTTI, even without inheritance, you might already
> have that pointer: so no additional cost)

But if your class were eg. the size of one pointer, then adding any virtual
functions would double its size.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.