← Back to team overview

dolfin team mailing list archive

Re: Multiple inheritance

 

On Mon, Nov 09, 2009 at 05:54:41PM +0100, Jed Brown wrote:
> Anders Logg wrote:
> >>>     I've narrowed one of the problems down to the following simple
> >>>     example. Say that a class C inherits from both A and B (which both
> >>>     have member variables - this is important). Then I would expect an
> >>>     object c of class C to have the same address/pointer as seen from all
> >>>     three classes.
> >> It could not possibly work this way because B must be able to function
> >> correctly with no knowledge of A and C.
> >
> > I don't understand why that would not work. It looks to me like it's
> > just some trick to simplify the cast from a C to a B, and that cast is
> > handled by an offset in the struct.
>
> Static casts are eliminated at compile time, they can't allocate memory
> to make copies of the data structure.  The definition of a struct
> completely determines it's layout in memory.  The definition
>
> class B
> {
> public:
>  B() { cout << "B pointer: " << this << endl; }
>  int bvar;
> };
>
> requires a particular layout, specifically sizeof B == sizeof bvar and
> offsetof(B,bvar) == 0.  If you give me a pointer to one of these things,
> I have to be able to use it this way.  The same applies to A, therefore
> it is not possible for (void*)(A*)&c == (void*)(B*)&c.

Thanks!

This makes it pretty clear. It also explains why (void*)(A*)&c == (void*)(C*)&c.

--
Anders

Attachment: signature.asc
Description: Digital signature


References