 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chandra Shekhar Kumar Guest
|
Posted: Thu Jun 26, 2003 10:31 pm Post subject: Re: can this be done in C? (a = new B, where B is an instanc |
|
|
| Quote: |
shape *D = new typeof(C);
|
you can use typeid
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Corey Murtagh Guest
|
Posted: Fri Jun 27, 2003 2:11 pm Post subject: Re: can this be done in C? (a = new B, where B is an instanc |
|
|
Chandra Shekhar Kumar wrote:
| Quote: |
shape *D = new typeof(C);
you can use typeid
|
Although the OP can indeed use typeid, there is no way that it will help
with the problem at hand. The typeid keyword returns a type_info const&
which is NOT the type. There is no simple way to create an instance of
a class from the type_info for that class.
Please, if you're going to give advice, don't waste everyone's time by
making it *BAD* advice.
--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Jun 27, 2003 2:12 pm Post subject: Re: can this be done in C? (a = new B, where B is an instanc |
|
|
In message <3EFA0E81.E9606F36 (AT) oracle (DOT) com>, Chandra Shekhar Kumar
<chandra.kumar (AT) oracle (DOT) com> writes
| Quote: |
shape *D = new typeof(C);
you can use typeid
|
Please demonstrate how you would use typeid to solve this problem.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Fri Jun 27, 2003 6:09 pm Post subject: Re: can this be done in C? (a = new B, where B is an instanc |
|
|
"Chandra Shekhar Kumar" <chandra.kumar (AT) oracle (DOT) com> wrote in message
| Quote: | shape *D = new typeof(C);
you can use typeid
|
No.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jun 30, 2003 10:35 am Post subject: Re: can this be done in C? (a = new B, where B is an instanc |
|
|
Francis Glassborow <francis.glassborow (AT) ntlworld (DOT) com> wrote
| Quote: | In message <3EFA0E81.E9606F36 (AT) oracle (DOT) com>, Chandra Shekhar Kumar
[email]chandra.kumar (AT) oracle (DOT) com[/email]> writes
shape *D = new typeof(C);
you can use typeid
Please demonstrate how you would use typeid to solve this problem.
|
if ( typeid( C ) == typeid( square ) ) {
D = new square( static_cast< square& >( C ) ) ;
} else if ( typeid( C ) == typeid( circle ) ) {
D = new circle( static_cast< circle& >( C ) ) ;
} else // ...
Not recommended, but it can be done.
Somewhat more flexible would be to use a std::map< std::type_info,
Factory >, or something along those lines, with each derived class
registering a factory. In practice, however, I've never found a case
where this was interesting. If you have an actual object that you want
to duplicate, the clone method is far better, and if you don't, where do
you get the type_info from -- you can't read it from a disk, or send it
over the network. If you need this, you have to define your own type
identification.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|