Thread View: comp.std.c++
4 messages
4 total messages
Started by Michael R. Gile
Fri, 17 Nov 1995 19:10
Q: RTTI and new??
Author: Michael R. Gile
Date: Fri, 17 Nov 1995 19:10
Date: Fri, 17 Nov 1995 19:10
25 lines
1545 bytes
1545 bytes
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 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? Thanks in advance. 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. ]
Re: Q: RTTI and new??
Author: 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. ]
Re: Q: RTTI and new??
Author: Michael R. Gile
Date: Mon, 20 Nov 1995 00:03
Date: Mon, 20 Nov 1995 00:03
62 lines
3677 bytes
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. ]
Re: Q: RTTI and new??
Author: David Vandevoorde
Date: Mon, 20 Nov 1995 16:34
Date: Mon, 20 Nov 1995 16:34
35 lines
2097 bytes
2097 bytes
>>>>> "MG" == Michael R Gile <gilem@rpi.edu> writes:
[...]
MG> as I said below, this is what I currently do. However, the
MG> application which I am working with uses template container
MG> classes. In order to make these containers, every class of every
MG> derivation tree of which there is a container must include these
MG> clone classes. This is a definite source for potential errors,
MG> since all you have to do is forget to create the clone member, and
MG> you have a problem. For my purposes it would be suffient to even
MG> know the size of the type which typeinfo represents. I just don't
MG> see the point in knowing the type, if you can't do anything with
MG> it but compare it to another object.
[...]
You can always check that the clone() member was successful:
template<typename T> inline
T&
clone(T const& original) {
T& duplicate = original.clone();
assert(typeid(original) == typeid(duplicate));
}
BTW, it's not too hard to achieve non-invasive clone-like
functionality using the `type_info' class (the `before()'
member helps too); you'd still need to register clonable
types with something like:
register_clonable_type<my_new_type>();
Daveed
[ 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads