Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!yale!slb-sdr!saito From: saito@sdr.slb.com (Naoki Saito) Newsgroups: comp.lang.c++ Subject: virtual assignment operator is possible? Keywords: assignment operator, virtual function Message-ID: <1989Dec30.174745.8174@sdr.slb.com> Date: 30 Dec 89 17:47:45 GMT Reply-To: saito@slb-sdr.UUCP () Organization: Schlumberger-Doll Research, Ridgefield CT Lines: 36 Suppose I have a base class B, and its derived class D. In each class, I defined assignment operator, i.e, X& operator=(X&). Then, B* pb0 = new B(); B* pb1 = new B(); D* pd0 = new D(); D* pd1 = new D(); *pb1 = *pb0 // B& operator=(B&) is invoked. *pd1 = *pd0 // D& operator=(D&) is invoked. This is perfectly fine. However, B* pdb0 = new D(); B* pdb1 = new D(); *pdb1 = *pdb0 // B& operator=(B&) is invoked. I want to use D& operator=(D&) in the above case. Of course, it is possible to do this by explicit casting: *((D*)pdb1) = *((D*)pdb0) // D& operator=(D&) is invoked. Can we invoke D& operator=(D&) without explicit casting? Even if I define B& operator=(B&) as a virtual function, this cannot be done because of the strong type checking, i.e., D& operator=(D&) cannot be binded to virtual B& operator=(B&). Am I asking too much? Regards, -- Naoki Saito (saito@sdr.slb.com) Schlumberger-Doll Research