[gnu.g++] initialization question

ct13+@andrew.cmu.edu (Chris Thewalt) (08/31/89)

if I have a class X, and a constructor X(X&) defined, and abc is an
instance of X,then
  X a = abc
works as described in the book.
However, if I have abc and def as instances of X, and operator + defined
as: X& operator+(X&, X&), then the following:
  X a = abc + def
does not call the constructor when creating a (I get bitwise copy of result).
Is this behavior correct?
[note: using GNU g++ version 1.35]

thewalt@ce.cmu.edu

zu@astbe.UUCP (Olaf Zurth) (09/12/89)

In article <QYzIoRy00iQJM8JkZb@andrew.cmu.edu> ct13+@andrew.cmu.edu (Chris Thewalt) writes:
>if I have a class X, and a constructor X(X&) defined, and abc is an
>instance of X,then
>  X a = abc
>works as described in the book.
>However, if I have abc and def as instances of X, and operator + defined
>as: X& operator+(X&, X&), then the following:
>  X a = abc + def
>does not call the constructor when creating a (I get bitwise copy of result).
>Is this behavior correct?
>[note: using GNU g++ version 1.35]

	You have an assigment and therefor you need an 

		operator=(X&)

	for your class. Bitwise copy is default for assigment.
	The best way is to build "save classes", which looks like

		class X {
		    // some stuff
		    X(something);
		    X(X&);
		    operator=(X&);
		    ~X();
		};

	--Olaf Zurth
	[see Stroustrup, p.180]
-- 
| Olaf Zurth            GEI Software Technik Berlin               Abt. A93-E2 |
| Hohenzollerndamm 150     D 1000 Berlin 33        Tel. (voice): +30 828 2892 |
| UUCP: zu@astbe.UUCP  olaf%zubln@astbe.UUCP  BITNET: zu%astbe@db0tui6.BITNET |