Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   casting from a string object (doing like Class.forName in java) (http://www.velocityreviews.com/forums/t459937-casting-from-a-string-object-doing-like-class-forname-in-java.html)

jean-francois 01-19-2007 05:36 PM

casting from a string object (doing like Class.forName in java)
 
In java you can do it with
Class.forName("myClass").newInstance();
this will return you a new instance of the class myClass, provided that
you defined it elsewhere. How can I get this in c++ (linux)?
For exemple I'm fetching elements from a vector, they are all
subclasses of a base class Field, but the data type within each
subclass can vary according to a template that was used (exemple
(SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
were stored/retrieved with the base class pointer Field since they are
taken from a vector
I can still define a common method "void * getData()" in the base class
field but I need to know how to recast once I fetch the values from the
vector if I'm doing some processing with the data.
I'm at least able to store the data name in a member variable, i.e.

SubField<T>: public Field
<template typename T>
setData( T data) {
m_data = data;
m_dataType = typeid(data).name();
}
But once I extract back this object from the vector the string
m_dataType that I can fetch from the Field *object is useless unless I
use a big switch ( I don't like it)
So how can one instanciate an object or define a cast type with only a
string descriptor?


Victor Bazarov 01-19-2007 06:19 PM

Re: casting from a string object (doing like Class.forName in java)
 
jean-francois wrote:
> In java you can do it with
> Class.forName("myClass").newInstance();
> this will return you a new instance of the class myClass, provided
> that you defined it elsewhere. How can I get this in c++ (linux)?


Yes, you can, if you implement it yourself. Basically, you need
a bunch of functions (factories) which would create your objects,
and then introduce a factory manager which will have every factory
named. Then you can call your manager's 'forNameNewInstance' and
get back a pointer (void*, most likely) to the newly created object.

> For exemple I'm fetching elements from a vector, they are all
> subclasses of a base class Field, but the data type within each
> subclass can vary according to a template that was used (exemple
> (SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
> were stored/retrieved with the base class pointer Field since they are
> taken from a vector
> I can still define a common method "void * getData()" in the base
> class field but I need to know how to recast once I fetch the values
> from the vector if I'm doing some processing with the data.
> I'm at least able to store the data name in a member variable, i.e.
>
> SubField<T>: public Field
> <template typename T>
> setData( T data) {
> m_data = data;
> m_dataType = typeid(data).name();
> }
> But once I extract back this object from the vector the string
> m_dataType that I can fetch from the Field *object is useless unless I
> use a big switch ( I don't like it)
> So how can one instanciate an object or define a cast type with only a
> string descriptor?


See above.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



I V 01-19-2007 10:49 PM

Re: casting from a string object (doing like Class.forName in java)
 
On Fri, 19 Jan 2007 09:36:17 -0800, jean-francois wrote:
> For exemple I'm fetching elements from a vector, they are all
> subclasses of a base class Field, but the data type within each
> subclass can vary according to a template that was used (exemple
> (SubField<int>, Subfield<long>,. Subfield<myclass>, etc..) The objects
> were stored/retrieved with the base class pointer Field since they are
> taken from a vector
> I can still define a common method "void * getData()" in the base class
> field but I need to know how to recast once I fetch the values from the
> vector if I'm doing some processing with the data.


You might find that Boost.Variant gives you a different (and possibly
better) way to solve your problem. See
http://www.boost.org/doc/html/variant.html



All times are GMT. The time now is 08:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.