🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2650

Re: Q: RTTI and new??

#2650
From: Michael R. Gile
Date: Mon, 20 Nov 1995 00:03
62 lines
3677 bytes
In article <martelli.816806336@cadlab>,
Alex Martelli <martelli@cadlab.it> wrote:
>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().

as I said below, this is what I currently do. However,  the application
which I am working with uses template container classes.  In order to 
make these  containers, every class of every derivation tree of which there
is a container must include these clone classes.  This is a definite
source for potential errors, since all you have to do is forget
to create the clone member, and you have a problem.  For my purposes it
would be suffient to even know the size of the type which typeinfo 
represents.   I just don't see the point in knowing the type, if
you can't do anything with it but compare it to another object.

>>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...

true, it is not overwhelmingly large, but it is a bit on the frustrating
side.  There a quite a few other features of C++ which I would glad to 
trade for a way to clone an existing object.  I guess I will just have to
work around it for now.

>Perhaps some template magic could help...?
>

Well, if anyone comes up with anything, then please post.


Mike
-- 
Michael Gile                        |               Graduate Assistant
gilem@rpi.edu                       |   Computer & Systems Engineering
"Don't Blame Me, I voted for Perot!"| Rensselaer Polytechnic Institute

[ 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: <48oeqi$tu3@litecdev.eng.rpi.edu>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <48i6qu$1825@litecdev.eng.rpi.edu> <martelli.816806336@cadlab>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: gilem@rpi.edu (Michael R. Gile)
Newsgroups: comp.std.c++
Subject: Re: Q: RTTI and new??
Date: 20 Nov 1995 00:03:59 GMT
Organization: Rensselaer Polytechnic Institute
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <48oeqi$tu3@litecdev.eng.rpi.edu>
References: <48i6qu$1825@litecdev.eng.rpi.edu> <martelli.816806336@cadlab>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: litecdev.eng.rpi.edu
Content-Length: 2854
Originator: clamage@taumet