casting from a string object (doing like Class.forName in java)

J

jean-francois

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?
 
V

Victor Bazarov

jean-francois said:
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
 
I

I V

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top