Topic: multiple inheritance and this pointer, a question


Author: pkt@lpi.liant.com (Scott Turner)
Date: Thu, 7 Oct 1993 15:56:51 GMT
Raw View
In article <1993Oct5.205124.6254@cc.ruu.nl>, reinder@neuretv.biol.ruu.nl (Reinder Verlinde) writes:
> I recently coded the following construction

> class B { B( int, A*); };
> class C: B, A { ... };
> C::C() : A(), B( 3, this) { ... }

> where 'this' is supposed to be a pointer to the 'A' part of the C to be
> constructed.
> I wondered, however, whether
>
>   - this was legal C++

Section 4.6 of the ARM (and likewise the standards committee's working paper)
support it, with the meaning "The result of the conversion is a pointer to
the base class sub-object of the derived class object."

But the ARM does not go into details of what the effect will be if the
derived class object is not present.  In your program, at the time
the B subobject is constructed, 'this' does not point to an object of
type C.  The legality of what you have done is not specified.

>   - all/most C++ compilers would eat and understand it, i.e. is it safe
> to use it?

I would expect so.  But most implementations use more machinery to support
virtual base classes.  If 'A' were a virtual base class of 'C' then it's
risky.

>   - is the order of evaluation of constructors defined?

Yes, but note that it's _not_ determined by the order of the base
initializers, i.e. the order is 'B' before 'A' because that's the
order in which they appear in C's base class list.
--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA    (508) 872-8700
UUCP: uunet!lpi!pkt                          Internet: pkt@lpi.liant.com




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 10 Oct 1993 17:01:13 GMT
Raw View
In article <1993Oct7.155651.6372@lpi.liant.com> pkt@lpi.liant.com (Scott Turner) writes:
>In article <1993Oct5.205124.6254@cc.ruu.nl>, reinder@neuretv.biol.ruu.nl (Reinder Verlinde) writes:
>> I recently coded the following construction
>
>> class B { B( int, A*); };
>> class C: B, A { ... };
>> C::C() : A(), B( 3, this) { ... }
>
>> where 'this' is supposed to be a pointer to the 'A' part of the C to be
>> constructed.
>> I wondered, however, whether
>>
>>   - this was legal C++
>
>Section 4.6 of the ARM (and likewise the standards committee's working paper)
>support it, with the meaning "The result of the conversion is a pointer to
>the base class sub-object of the derived class object."
>
>But the ARM does not go into details of what the effect will be if the
>derived class object is not present.  In your program, at the time
>the B subobject is constructed, 'this' does not point to an object of
>type C.  The legality of what you have done is not specified.

 It wouldnt matter if it did or not, since there is no
workable definition of what an object is. <g>

 By the C definition, a pointer of type C* always points
to an object of type C. (Unless its 0)

 I think its should always be possible upcast anywhere
in a constructor, including the arguments to any mem-initialisers.

 If the cast is to a non-virtual base, its offset is known.

 If the cast is to a virtual base, then:

 a) if we are the most derived class, the offset is known

 b) if we arent, the virtual base is already initialised,
    and the known location containing the offset (virtual
    base pointer) is already initialised.

   (It has to be, because if it isnt, it never will be:
    'we' certainly cant initialise it)


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: reinder@neuretv.biol.ruu.nl (Reinder Verlinde)
Date: Tue, 5 Oct 1993 20:51:24 GMT
Raw View
I recently coded the following construction (omitting some 'public's
and the like)

class A
{
    A()
    ....
}

class B
{
    B( int, A*);
    ....
}

class C: B, A
{
    C()
}

with source:

C::C() : A(), B( 3, this)
{
    ...
}

where 'this' is supposed to be a pointer to the 'A' part of the C to be
constructed.
I wondered, however, whether

  - this was legal C++
  - all/most C++ compilers would eat and understand it, i.e. is it safe
to use it?
  - is the order of evaluation of constructors defined? Logic would
dictate it not to be
    since there is a comma between constructors, but I know that
analogy is rotten since
    C::C() : A(); B( 3, this)
    would clearly err.

gcc did compile the code, but the resulting program crashed. I did
_not_ have time
to delve into this, so I just wrote it differently, also modifying some
other things.
My question therefore remains: is this legal C++?

Reinder Verlinde




Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: Thu, 7 Oct 1993 01:51:51 GMT
Raw View