🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

overloading the class assignment operator

#1004
From: Buz Owen
Date: Mon, 01 Apr 1991 22:24
45 lines
1680 bytes
The following program is illegal in C++:

class B
{
  int i;
  int j;
public:
  operator = (int);
}

class D : public B
{
}

main()
{
  D d;
  d = 1;		// error
}

because the assignment operator is not inherited by a subclass.  I
submit that this constraint can be usefully relaxed to apply only to
any of the four operators:

	B::operator = ( [const] B [&] )

If the constraint were relaxed as I suggest, and a class B defined
operator = members other than one of the above, they would be
inherited by subclasses of B.  The compiler would still compose the
subclass assignment opeator(s) according to the present rules, using the
automatically generated or explicitly defined base class assignment
operators (one of the 4 above) to copy the base class members, and
then performing memberwise assignment of members unique to the derived
class.

As an example in which overloading assignment to accept rhs arguments
of other than the class type might be useful, consider a Timer base
class in which assignment is overloaded to set the timeout value of
the timer, with the side effect of starting the timer running.


Buz Owen
BBN Systems and Technologies
Cambridge, Mass.
ado@bbn.com
Technical Details
Message-ID: <63493@bbn.BBN.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!wuarchive!mit-eddie!bbn.com!ado

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!wuarchive!mit-eddie!bbn.com!ado
From: ado@bbn.com (Buz Owen)
Newsgroups: comp.std.c++
Subject: overloading the class assignment operator
Keywords: overloading, class assignment operator
Message-ID: <63493@bbn.BBN.COM>
Date: 1 Apr 91 22:24:14 GMT
Sender: news@bbn.com
Reply-To: ado@BBN.COM (Buz Owen)
Organization: Bolt Beranek and Newman Inc., Cambridge MA
Lines: 45