Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!snorkelwacker.mit.edu!ira.uka.de!t500i0!fuchs From: fuchs@t500i0.telematik.informatik.uni-karlsruhe.de (Harald Fuchs) Newsgroups: comp.lang.c++ Subject: Re: assignment operator for dervired classes Message-ID: Date: 24 Apr 91 23:19:49 GMT References: <1991Apr23.153754.4582@netcom.COM> Sender: news@ira.uka.de (USENET News System) Organization: University of Karlsruhe, FRG Lines: 58 aed@netcom.COM (Andrew Davidson) writes: >I have been having trouble writing an assignment operator for a >derived >class. The problem is how do you initialize the the base class. The >reading >I have done to date suggest that that the assiginment operator for a >derived >class is written the same way the copy constructor is written. >#include >class X >{ > public: > X(int i = 0) > {numX = i;} > X(const X &other) > {numX = other.numX;} > X& operator = (const X &other) > { > numX = other.numX; > return (*this); > } > void display() {cout << "numX = " << numX << " ";} > private: > int numX; >}; >class Y : public X >{ > public: > Y(int i = 1) : X(i) > {numY = i;} > Y(const Y &other) : X(other) > {numY = other.numY;} > Y& operator = (const Y &other) > { >/* 49 */ X(other); > numY = other.numY; > return(*this); > } > void display() {X::display(); cout << "numY = " << numY << "\n";} > private: > int numY; >}; Use the assignment operator of the base class. Simply replace line 49 by X::operator= (other); -- Harald Fuchs