Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!apple!netcom!aed From: aed@netcom.COM (Andrew Davidson) Newsgroups: comp.lang.c++ Subject: assignment operator for dervired classes Message-ID: <1991Apr23.153754.4582@netcom.COM> Date: 23 Apr 91 15:37:54 GMT Organization: Netcom - Online Communication Services UNIX System {408 241-9760 guest} Lines: 95 /* Hi 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. The problem is that I can not call the base class constructor from within the derived class assignment operator. I get the following message from the compiler "assign.cc", line 49: warning: result of constructor call expression not used */ #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; }; /* * out put is * numX = 2 numY = 2 * numX = 3 numY = 3 * numX = 2 numY = 3 this line should be 3,3 * numX = 3 numY = 3 */ main() { Y y1(2); y1.display(); Y y2(3); y2.display(); y1 = y2; y1.display(); Y y3(y2); y3.display(); } -- ----------------------------------------------------------------- "bede-bede-bede Thats all Folks" Porky Pig Andy Davidson Woodside CA. aed@netcom.COM -----------------------------------------------------------------