From -32755143915968237
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8daacd4ea4200eea
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1993-07-22 10:45:09 PST
Path: gmd.de!newsserver.jvnc.net!yale.edu!spool.mu.edu!uwm.edu!linac!att!att!allegra!alice!ark
From: ark@alice.att.com (Andrew Koenig)
Newsgroups: comp.std.c++
Subject: Re: Constructing A from B
Keywords: constructor
Message-ID: <26066@alice.att.com>
Date: 20 Jul 93 16:28:22 GMT
Article-I.D.: alice.26066
References: <CAGts0.372@world.std.com>
Reply-To: ark@alice.UUCP ()
Organization: AT&T Bell Laboratories, Murray Hill NJ
Lines: 22

In article <CAGts0.372@world.std.com> smayo@world.std.com (Scott A Mayo) writes:

> The "solution" I hit on, and I don't like it, is
>     *(A*)this = *a;
> which does in fact assign all the values over for the in-common member
> variables. It works because, at least in Microsoft C7, the order of
> member variables in a derived class instance puts the base class first.

No, it works because C++ defines conversion of a derived class pointer
to a base class pointer as an operation that yields a pointer that points
to the base class part of the derived class object.

The technique above is well-defined and portable.

A slightly shorter way to write it might be

	((A&) *this) = *a;

where the outer parentheses are actually unnecessary.
-- 
				--Andrew Koenig
				  ark@research.att.com


