473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?

Jan 19 '07 #1
2 2415
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
Jan 19 '07 #2
I V
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

Jan 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Davison | last post by:
I need to load a different version of a class than what is being used by the overall system. My test code works fine for loading the class, but fails when I try to cast the Object. For example, the...
2
by: Sebastien Degardin | last post by:
Hello, I need to use a Factory pattern to create services class. i have an interface --> MyService i can have an abstract class --> MyAbstractService i have several concrete class for this...
3
by: Jeff Poste | last post by:
Hi, I'm developing software that requires business rules that constantly need new versions for different quarters, years, etc. I created a business rule factory that stores these different...
9
by: Kris | last post by:
Hi. I have a asp class A with a public function AA() ----------------A.aspx.cs------------------------ public partial class A : System.Web.UI.Page { protected void Page_Load(object sender,...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and virtual inheritance to implement interfaces. I ran...
4
by: PaperPilot | last post by:
I have a Java program that is deployed as a .jar file in which is another .jar file with the mysql library. Here is the appropriate section of code: /** * Makes the connection to the...
4
by: robtyketto | last post by:
Greetings, I'm a newbie to Java and I cant quite get my head around why it seems most integers appears to be formatted as strings and then later have to be parsed. The code below, passes in the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.