Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!homxb!mtuxo!mtune!rutgers!bellcore!faline!ulysses!allegra!alice!bs From: bs@alice.UUCP Newsgroups: comp.lang.c++ Subject: Re: virtual constructors Message-ID: <7331@alice.UUCP> Date: Wed, 30-Sep-87 21:26:35 EDT Article-I.D.: alice.7331 Posted: Wed Sep 30 21:26:35 1987 Date-Received: Sat, 3-Oct-87 09:21:43 EDT References: <895@saturn.ucsc.edu> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 36 Keywords: constructors, derived Summary: how to do it # From: conrad@jupiter.ucsc.edu.UUCP (conrad @ University of California, Santa Cruz; CIS/CE) # Virtual constructors are not allowed. So if I need a a copy # of a particular object which has derived classes, how do I # do it? For example: rectangle, circle, line, etc. are derived # classes of the base class *shape*. Now I want to make a copy # of a given shape. I think you are just thinking about it a little bit wrong. YOU cannot make a copy of something that you don't know the exact type of, but with a little bit forethought you can ask IT to copy itself; after all IT (and nobody else) knows exactly what kind of shape it is (provided it has suitable virtual functions): class shape { // ... virtual shape* copy(); }; class circle : public shape { // ... shape* copy() { return new circle(*this); } }; class rectangle : public shape { // ... shape* copy() { return new rectangle(*this); } }; my_program(shape* p) { shape* p2 = p->copy(); // make a copy of p // ... }