TITLE: virtual assignment and derivation [ There are a bunch of other tips relating to the assignment operator. See the link from http://www.ses.com/~clarke ] PROBLEM: Ross Smith How do you get a virtual assignment operator to work properly? RESPONSE: kanze@lts.sel.alcatel.de (James Kanze), 7 Dec 95 I'm not sure that this is the answer Ross Smith was looking for. Consider the following (B is base, D1 and D2 derive from B): B* pd1 = new D1 ; B* pd2 = new D2 ; *pd1 = *pd2 ; How would you make this work? (IMHO, it basically requires double dispatch, and even then, it apparently involves changing the type of *pd1, which is not allowed.) In practice, I've found the best solution for classes with inheritance is simply to ban assignment, making operator= private. I then provide a clone virtual function, and let the user decide exactly what he wants: B* pd1 = new D1 ; B* pd2 = new D2 ; pd1 = pd2 ; // shallow copy pd1 = pd2->clone() ; // deep copy, including // type change. (Obviously, in such cases, I either link in the Boehm garbage collector or use reference counted pointers instead of built-in pointers.) Assignment and polymorphism don't seem to work very well together.