Getting the name and type of a struct field from its object

For example, I have a structure that looks something like this:

struct Test
{
    int i;
    float f;
    char ch[10];
};

And I have an object of this structure, for example:

Test obj;

Now I want to programmatically get the field names and type obj. Is it possible?

This is C ++ BTW.

+1
source share
4 answers

You are requesting Reflection in C ++ .

+9
source

I am afraid that you cannot get the field names, but you can get the obj type using Boost.Typeof :

#include <boost/typeof/typeof.hpp>
typedef BOOST_TYPEOF(obj) ObjType;
+4
source

, "" . sizeof , . ++ , , . , "GetNumMembers()" "GetMemberSize (index)" .., , ...

+1

" ++", Boost. Qaru " ++".

++ . Deserialization .

My experience, requiring class and member names, comes from printing debugging information. Class and field names would be useful in handling exceptions, especially when they are thrown.

0
source

All Articles