Article View: comp.std.c++
Article #2644Re: Q: RTTI and new??
From: Alex Martelli
Date: Sun, 19 Nov 1995 19:07
Date: Sun, 19 Nov 1995 19:07
43 lines
2689 bytes
2689 bytes
gilem@rpi.edu (Michael R. Gile) writes: ... >is there any way to use the typeinfo which is returned from typeid >to create a new object of that type? I have been looking at the >draft, and this doesn't seem to be possible. The reason that I ask >is that it is very useful when you have a container full of things >to be able to duplicate the container. If all of the objects Actually, this specific task seems to call for a virtual clone() function, one which copies a *specific* object (the canonical form for it is I guess "return new T(*this);" for any T); if typeinfo were to include a "factory method", it surely would NOT also carry around all the instance-data needed for this anyway, in other terms it would at most obviate the need for a virtual make() (canonical form "return new T();"), which has its place but is of much rarer occurrence than clone(). >are not of the same type (maybe they are derived classes), then >it is not easy to duplicate each object. Currently, my code gets >around this by making a virtual function for each object that needs >to duplicate itself, but I was hoping that there would be a standard >way to do this now that we have RTTI. Am I the only one who thinks >that this feature would be incredibly useful? Oh, no, the "exemplar idiom", as e.g. Coplien calls it, is indeed quite often used; but the clone() form of it is the most frequent, having to do more with a specific object than with just its _type_ -- and the amount of boilerplate to write for it, though non-null, is not huge either... Perhaps some template magic could help...? Alex -- DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected in any way with my employer, nor any other organization or individual! Email: martelli@cadlab.it Phone: +39 (51) 597313 CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia Fax: +39 (51) 597120 [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<martelli.816806336@cadlab>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<48i6qu$1825@litecdev.eng.rpi.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: martelli@cadlab.it (Alex Martelli) Newsgroups: comp.std.c++ Subject: Re: Q: RTTI and new?? Date: 19 Nov 1995 19:07:40 GMT Organization: Cad.Lab Spa, Bologna, Italia Sender: news@cadlab.it Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <martelli.816806336@cadlab> References: <48i6qu$1825@litecdev.eng.rpi.edu> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Newsreader: NN version 6.5.0 #11 Content-Length: 1903 Originator: clamage@taumet