7

I have a struct definition with about 25 elements

struct X { field 1; field 2; .. };    

and I'm trying to fill it with some map values

Map<String,String> A    

and it appears to be very annoying to do such thing n times

X->xx = A["aaa"]    

every time that I want to fill my message struct.

Is it possible to access the members by name, e.g.

X->get_instance_of("xx").set(A["aaa"]);    

and put it into a loop?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Skan
  • 71
  • 1
  • 2

5 Answers5

9

C++ lacks built-in reflection capabilities of more dynamic languages, so you cannot do what you would like using he out of the box capabilities of the language.

However, if all members are of the same type, you can do it with a map of pointers to members and a little preparation:

 // typedef for the pointer-to-member
 typedef int X::*ptr_attr;

 // Declare the map of pointers to members
 map<string,ptr_attr> mattr;
 // Add pointers to individual members one by one:
 mattr["xx"] = &X::xx;
 mattr["yy"] = &X::yy;

// Now that you have an instance of x...
 X x;
// you can access its members by pointers using the syntax below:
 x.*mattr["xx"] = A["aa"];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 3
    Even if they're different types, it can be done with function overloading. – Nawaz Feb 27 '12 at 16:54
  • @Nawaz Sure, but that would require an entirely different solution. Moreover, it is very likely that such solution would be more elegant than the one I proposed. – Sergey Kalinichenko Feb 27 '12 at 16:58
  • Yes. One such solution is given by [@Johannes Schaub](http://stackoverflow.com/users/34509/johannes-schaub-litb) here : http://stackoverflow.com/a/287353/415784 – Nawaz Feb 27 '12 at 17:27
  • Question: `&X::xx;` in this line, `X` is not instantiated yet, but the address exist? How is this possible? – starriet Jun 15 '23 at 22:53
  • 1
    @starriet `X` is a type, so it does not go through an initialization (instances of `X`, such as `x`, go through an initialization, though). Pointer-to-member works like a glorified offset: C++ accesses `mattr["xx"]` for the "offset", operator `.*` adds it to the address of `x` and dereferences it, producing a reference to the corresponding member. – Sergey Kalinichenko Jun 16 '23 at 01:49
  • @SergeyKalinichenko Thanks! Is `.*` itself a special operator, not the same as `.` and `*` combined? So, `.*` works something like `*((&x) + mattr["xx"])`? – starriet Jun 16 '23 at 04:42
  • 1
    @starriet Correct, [member access operators `.*` and `->*`](https://en.cppreference.com/w/cpp/language/operator_member_access) are separate operators. – Sergey Kalinichenko Jun 16 '23 at 11:30
2

Short answer: no. This is C++, a statically compiled language, where the structure member names are converted by the compiler into memory offsets. It is not dynamic like PHP or Python where the runtime is involved with all variable references.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

No. C++ doesn't have reflection. Java does though. Unsurprisingly, SOA related stuff is more probably encountered with languages like Java than it is with languages like C.

foxx1337
  • 1,859
  • 3
  • 19
  • 23
  • 1
    Not sure that’s true. SOAP APIs in Java generate code wrappers as a compilation step. Why shouldn’t the same exist for C++? There’s no need to use reflection for that. – Konrad Rudolph Feb 27 '12 at 17:05
  • It surely can be done in C++ as well, with member pointers and function overloads. – Nawaz Feb 27 '12 at 17:07
1

It's not really possible to do that; the information you need is no longer present at runtime. You might be able to do something with a map and some pointers, but to be honest you would probably be better off just wrapping it up in a function that takes a map and puts the values into the appropriate fields.

spencercw
  • 3,320
  • 15
  • 20
0

You could create a wrapper objet for your struct with set/get accessors that will let you iterate over string values for filling/reading the underlying struct that is static.

dweeves
  • 5,525
  • 22
  • 28