I can address this:

current_average_speed_element->in_bytes 

How do I contact so:

 string myvar = "in_bytes"; //... current_average_speed_element->myvar; 

All code in its entirety:

 #include <fstream> #include <stdio.h> #include <typeinfo> #include <vector> using namespace std; class map_element { public: map_element() : in_bytes(0){ } void set(int val){in_bytes=val;} uint64_t in_bytes; }; int main() { vector<map_element> current_element; current_element.resize(1); current_element[0].set(199999); map_element* current_average_speed_element; current_average_speed_element = &current_element[0]; string myvar = "in_bytes"; //cout << "OUT RESULT: " << current_average_speed_element->{myvar} <<endl; return 0; } 
  • Give the code of your class and how you work with this class. - aleks.andr
  • one
    Why do you need it? C ++ is not PHP or Javascript, it has other expressive means. What problem do you want to solve with a name call? - VladD
  • The fact is that there are several characteristics with fixed values: protocol: tcp|udp|.... direction: incoming|outcoming flags: SYN|ACK|... for each value each character has its own counter: element->tcp_syn_incoming ... so not to go through all the possible options, I plan to generate this very key = tcp_syn_incoming - fizikst
  • , the hakarteristiks are set via json and can be different: tcp_syn_incoming, tcp_ack_incoming, tcp_fin_incoming ... or they cannot be at all, otherwise a large number of if and s will be obtained - fizikst
  • your own counter: element-> tcp_syn_incoming - what kind of variable is it? - Grundy

1 answer 1

What you are trying to do is called Reflection (reflection) , but reflection is not supported in C ++.

It is possible to refer to a pointer to a member of a class, but not by its name in the string

Example with pointer

 #include <iostream> using namespace std; class A{ public: int member; }; using a_member = int A::*; int main() { a_member m = &A::member; // получаем указатель на член класса //... A* a = new A(); a->member = 42; // обращаемся по указателю cout << (a->*m) << endl; delete a; return 0; } 

ideone

Based on this, you can create a dictionary (map) "name" -> "pointer"

Minimum implementation:

 #include <iostream> #include <string> #include <unordered_map> struct A { int first; int second; }; using member_ptr = int A::*; using member_map = std::unordered_map<std::string, member_ptr>; static member_map Amap = { {"first", &A::first}, {"second", &A::second} }; int main() { A a; a.*(Amap["first" ]) = 42; a.*(Amap["second"]) = 24; std::cout << a.first << std::endl << a.second << std::endl; return 0; } 

ideone

  • Thanks for the answer, but this does not solve the problem, if I understood everything correctly, this case is equivalent to the case of a direct appeal by property, I think I will have to fight back if-s. - fizikst
  • @fizikst theoretically, you can create a dictionary (map) "name" - "pointer" and use it to refer to obj->*(map[name]) but this construct looks like “scary” - Maxim Timakov
  • @fizikst is not exactly equivalent: usually the code is more compact and - with a large table size - more understandable than overgrown if and switch blocks. The initialization of the table is simply encapsulated into a function with a talking name and hidden from sight, simplifying the remaining code. It is necessary to look, whether the code becomes simpler in your specific case. - Ildar Khairullin
  • @ IldarHayrullin just the code with the table was added after this comment) initially the answer was only about the pointer - Maxim Timakov