Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!apple!ames!pasteur!ucbvax!ulysses!hector!jss From: jss@hector.UUCP (Jerry Schwarz) Newsgroups: comp.lang.c++ Subject: Re: More Questions on operator= Keywords: inheritance and assignment, default behaviour of assignment Message-ID: <11549@ulysses.homer.nj.att.com> Date: 18 May 89 19:12:01 GMT References: <925@ethz.UUCP> Sender: netnews@ulysses.homer.nj.att.com Reply-To: jss@hector.UUCP (Jerry Schwarz) Organization: AT&T Bell Laboratories Lines: 57 In article <925@ethz.UUCP> marti@ethz.UUCP (Robert Marti) writes: > > >On a related issue, what is the approved way to get the default >behaviour of assignment (ie., bitwise copying) INSIDE the definition >of a user-defined operator=? No. And in 2.0 the default behavior never copies the _vptr. The _vptr is associated with an object when the object is allocated and cannot be changed. Copying the _vptr "breaks" the C++ type system. Consider something like class B { virtual void f() { } } ; class D : class B int x ; void f() { x = 99 ; } } ; void g() { B b ; D d ; b = d ; // legal because D is derived from B. b.f() ; // If the vptr has been copied then this // call will be nonense, b has no x member // for f to store 99 in. } >(I need to do this to make sure that >an invisible _vptr field is alway copied along with all data members.) >::operator=(*this, another) does not work (why?), so I currently use >bcopy((char*)&another, (char*)this, sizeof(Object)) instead, e.g. > bcopy (or the ANSI equivalent memcpy) is the appropriate way to do this operation. Just be sure you know what you're doing when you circumvent the type system this way. >Object& operator=(Object& another) >{ > // do some stuff (e.g., manipulating reference counts) > bcopy((char*)&another, (char*)this, sizeof(Object)); // copy EVERYTHING > // do some more stuff > return *this; >} This example doesn't look like an appropriate use. It is subject to the same "bug" as my earlier example. What if the "another" was allocated as a D (where D is a class derived from Object)? Jerry Schwarz AT&T Bell Labs, Murray Hill