🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2275

Re: Proposal for fixing a problem with default assignment

#2275
From: John Max Skaller
Date: Sun, 22 Oct 1995 06:58
76 lines
2815 bytes
William Sears <sears@zork.tiac.net> wrote:
>The current default definition of assignment for C++ produces 
>incorrect code in most cases where either a copy constructor or
>a destructor present, thus requiring the programmer to override
>the default

>The proposed change:
>
>
>       If there is:
>
>              a.  No assignment operator
>              b.  Either a user defined copy constructor
>                    OR Defined destructor.
>          
>       Then the definition of Class operator=(const Class& other) is:
>
>               if (this==&src)
>                   return *this;
>               Class::~Class();
>               new (this) Class(src);
>               return *this;
>

    Unfortunately, this definition of assignment will 
badly corrupt programs using inheritance. The reason
hinges on the fact that constructors install virtual table
pointers -- asssignment operators do not affect virtual tables.

    To see the problem, consider:

    struct B { 
       virtual void something();
       B(B const &) {}
       virtual ~B() {}
       // generated assignment
   };

   struct D : B { virtual void something(); };

   D d1, d2;
   B &b1 = d1, &b2 = d2;
   b1 = b2; // slicing

Slicing is dangerous anyhow, because any data in the LHS
derived class is not changed by the assignment --
assignment operators in bases should be protected to
prevent this happening.

However, the effect of the current generated assignment
in these circumstances is to mess up USER CLASS INVARIANTS.

The effect of the proposal WOULD CHANGE THE TYPE OF THE OBJECT.
A much more serious problem.

The problem with the proposal is that one would have to
override the default in base classes, whereas with the
status quo one overrides in derived classes.

[In favour of the proposal is that it doesn't
assign bases twice -- it works well for leaf classes]

-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au




---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <46bkdv$qnr@oznet07.ozemail.com.au>
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt
References: <3086FA30.4B29@zork.tiac.net>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!Matt
From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Newsgroups: comp.std.c++
Subject: Re: Proposal for fixing a problem with default assignment
Date: 22 Oct 1995 06:58:12 GMT
Organization: MAXTAL
Approved: Matt@Austern
Message-ID: <46bkdv$qnr@oznet07.ozemail.com.au>
References: <3086FA30.4B29@zork.tiac.net>
NNTP-Posting-Host: austern.hip.berkeley.edu