Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 3rd, 2008, 10:55 PM
cutecutemouse
Guest
 
Posts: n/a
Default How to call a function by a string of name & How to get the name of aclass?

---------------------------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.



  #2  
Old July 3rd, 2008, 11:25 PM
Sam
Guest
 
Posts: n/a
Default Re: How to call a function by a string of name & How to get thename of a class?

cutecutemouse writes:
Quote:
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.
No, there is no equivalent of Java-like reflection classes in the standard
C++ language. The only thing that's available in the standard C++ language
is a facility for retrieve an "implementation-defined" name of the object's
class, which may not necessary be the human-readable class name.

Having said that, your C++ compiler may or may not have its own
compiler-specific facilities for run-time type information that provides
more information than the C++ language standard requires. Check your
compiler's documentation for more information.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhtXh4ACgkQx9p3GYHlUOI+sgCffGkGm9Mttj gyy+UM4LlXSjGv
C4IAn0dz+SoTOg5pBHxMRwojL5HoB140
=flFV
-----END PGP SIGNATURE-----

  #3  
Old July 4th, 2008, 12:05 PM
Pascal J. Bourguignon
Guest
 
Posts: n/a
Default Re: How to call a function by a string of name & How to get the name of a class?

cutecutemouse <plhalice@hotmail.comwrites:
Quote:
---------------------------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 Classstd::string getClass(Class* object){
return unmangle(typeid(*object).name());
}

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

Quote:
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.

--
__Pascal Bourguignon__
  #4  
Old July 13th, 2008, 09:35 PM
cutecutemouse
Guest
 
Posts: n/a
Default Re: How to call a function by a string of name & How to get the nameof a class?

I've found a third-party Reflection API "Reflex" for it. Thank you!

On 4 Jul., 01:17, Sam <s...@email-scan.comwrote:
Quote:
cutecutemouse writes:
Quote:
1, How do I get the name of a class? Just like by Java the
method .getclass() or instanceof ?
>
Quote:
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?
>
Quote:
Thank you very much for your reply, indeed.
>
No, there is no equivalent of Java-like reflection classes in the standard
C++ language. The only thing that's available in the standard C++ language
is a facility for retrieve an "implementation-defined" name of the object's
class, which may not necessary be the human-readable class name.
>
Having said that, your C++ compiler may or may not have its own
compiler-specific facilities for run-time type information that provides
more information than the C++ language standard requires. Check your
compiler's documentation for more information.
>
application_pgp-signature_part
1KHerunterladen
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.