Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name and type of a struct field from its object

Tags:

c++

struct

For example, I have a struct which is something like this:

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

And I have an object of this struct such as:

Test obj;

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

This is C++ BTW.

like image 311
Aamir Avatar asked Dec 05 '22 03:12

Aamir


2 Answers

You are asking for Reflection in C++.

like image 137
Björn Pollex Avatar answered Jan 18 '23 21:01

Björn Pollex


I'm afraid you cannot get the field names, but you can get the type of obj using Boost.Typeof:

#include <boost/typeof/typeof.hpp>
typedef BOOST_TYPEOF(obj) ObjType;
like image 42
Sebastian Avatar answered Jan 18 '23 22:01

Sebastian