How to get names and types of struct elements?

Hello Rooters,

If I have a structure such as:
struct {
TString var1;
float var2;
int var3;
} thing;

how can I enumerate the element names and types within this structure?

I’m pretty sure this is a rootcint kind of thing, but I need a little shove in the right direction.

thanks very much,


I am not sure if there is some way to have CINT give you this kind of access, but there are no reliable way to do this with standard C++ (i.e. there is no reflection in C++). You could however use a heterogeneous container like tr1::tuple which allows you to store different types.

#include <iostream>
#include <tr1/tuple>

typedef std::tr1::tuple<int, bool, double> MyT; // store an int, a bool & a double

int main() {
  MyT t(1.2, 1.2, 1.2); // tuple containing int(1.2), bool(1.2), double(1.2), i.e. 1, true, 1.2
  using std::tr1::get;
  std::cout << get<0>(t) << std::endl;
  std::cout << get<1>(t) << std::endl;
  std::cout << get<2>(t) << std::endl;

I want to do this by using cint to make a dictionary of my structure.

[quote]I want to do this by using cint to make a dictionary of my structure.[/quote]This is exactly the job the dictionary generation and TClass where designed for. So generate a dictionary for your struct (via ACLiC is the simplest) then you can get a TClass object that will describe your struct and your for example call GetListOfDataMembers() etc…