How to call a function by a string of name & How to get the name of aclass?

C

cutecutemouse

---------------------------This is my
task:--------------------------------------
There are hundred of similar classes and a large file(in processing)
with thousand of instances of these classes. I should export these
instances into a XML file (I use Tiny XML), however not total
instances, but these which belong to the selected classes and the
selected attributes(member variable and value). In advance, I will
have a configuration file (also XML format), to define which classes
and which attributes we need, that is a user-defined file.
The problem are:

1, How do I get the name of a class? Just like by Java the
method .getclass() or instanceof ?

2, Is it possible to have such as an iterator to traverse through
all the variables/functions, to only call the functions and to get
the values of variables we need by name?

Thank you very much for your reply, indeed.
 
P

Pascal J. Bourguignon

cutecutemouse said:
---------------------------This is my
task:--------------------------------------
There are hundred of similar classes and a large file(in processing)
with thousand of instances of these classes. I should export these
instances into a XML file (I use Tiny XML), however not total
instances, but these which belong to the selected classes and the
selected attributes(member variable and value). In advance, I will
have a configuration file (also XML format), to define which classes
and which attributes we need, that is a user-defined file.
The problem are:

1, How do I get the name of a class? Just like by Java the
method .getclass() or instanceof ?

Using RTTI:

template <class Class> std::string getClass(Class* object){
return unmangle(typeid(*object).name());
}

You will have to implement the unmangle function for your compiler...

2, Is it possible to have such as an iterator to traverse through
all the variables/functions, to only call the functions and to get
the values of variables we need by name?

You'll get no help from C++, but it's possible.

Here are two ways:

- use the source (parse it, or use the UML model if you design your
program from an UML CASE tool), and generate from it the meta
objects needed to do it.

- compile your program with a lot of debugging information (-ggdb3
-gdwarf-2 etc), and then use these debugging information to do
introspection at run-time, like any debugger would do it.

Here is also a third way to solve your problem, since you know before
hand what class and what member you need to access (from your xml
configuration files), you can use them to generate the C++ code that
will do the job.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top