0

how to get class member by string? have .h:

class configFile {
public:
    class cfgVehicles {
    public:
        class base_car {
        public:
            int speed = 80;
            FString displayName = "unknown";
        };
        class uazVehicle : public base_car {
        public:
            FString displayName = "uaz";
        };
        class uazVehicle2 : public uazVehicle {
        public:
            FString displayName = "uaz2";
        };
        class uralVehicle : public base_car {
        public:
            FString displayName = "ural";
        };
    };
};

metod to get int class member:

configFile::cfgVehicles::uazVehicle2 dataObj;
configFile::cfgVehicles::uazVehicle2* const ptrDataObj = &dataObj;
int getInt = ptrDataObj->speed;
returt getInt;

how i can get speed class member by string? tried so - no work

std::string whatGet = "speed";
int getInt = ptrDataObj->*whatGet;
hekut146
  • 23
  • 3
  • 4
    You must create such code that explicitly maps strings to members, there's no way to do it automatically. – Some programmer dude Feb 03 '21 at 22:47
  • I don't understand what you mean, could you provide an example? – hekut146 Feb 03 '21 at 22:56
  • 2
    There's no [reflection](https://en.wikipedia.org/wiki/Reflective_programming) in C++ – m88 Feb 03 '21 at 22:57
  • Are you talking about the string switch and all the pre-written options? 2. This function is only for get numbers, I will not know in advance what class members names can be... – hekut146 Feb 03 '21 at 22:59
  • Looks like you're coming into C++ from a language sitting atop a virtual machine or other complex runtime. To quote Master Yoda, “You must unlearn what you have learned." Just like C++ [doesn't allow you to get the names of classes and other types or inheritance hierarchies at runtime](https://stackoverflow.com/questions/66017121/how-to-get-parent-class-name-from-child), the member's identifiers are likewise stripped away during compilation. The computer doesn't need those names. All if cares about is where the member is relative to where the containing object is. – user4581301 Feb 03 '21 at 23:05
  • @user4581301 Thanks for the clarification – hekut146 Feb 03 '21 at 23:16
  • A note to head off a future question: In `base_car` you have `FString displayName`. In `uazVehicle` you have `FString displayName`. These will be different variables. `uazVehicle` will have a `displayName` member that *hides* the `displayName` it inherited from `base_car`. Likely you only want one `displayName` variable, the one defined in `base_car`. To do this you need to learn about the [Member Initializer List](https://en.cppreference.com/w/cpp/language/constructor). Example: https://ideone.com/ooJc9y . – user4581301 Feb 03 '21 at 23:37
  • I strongly recommend getting [a good set of C++ references and an introductory text](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ is unforgiving when it comes to guesswork and assumptions based on other languages, and the Internet is next to useless when it comes to looking for help until you know the language well enough to recognize when a tutorial is feeding you bad information. – user4581301 Feb 03 '21 at 23:40
  • @user4581301 thanks for link, I understand this, this is the only reason why I need this class structure, (inheritance), so that the child element (derivative) inherits some parameters from the parent (base class) and can override some class members – hekut146 Feb 03 '21 at 23:47
  • This is need to adequately fill the table, in game engine have CSV table, no support inheritance, i wanna try write all data in this class structure, to get all params and classes to refill game engine CSV table with inheritance support – hekut146 Feb 03 '21 at 23:52
  • I don't recommend learning C++ by writing a video game. Learn C++, then write the video game. C++ is a complicated language, possibly the most complicated language in regular use, and even if you do manage to learn it by developing something complicated, it will be an extremely slow, frustrating experience. I came into C++ from a background in C and Java. I am still finding and fixing craptacular code from a three year period where I thought that knowing C and Java meant I knew C++. The code form that era mostly works. And then suddenly it doesn't. – user4581301 Feb 04 '21 at 00:00
  • @user4581301 this game engine (unreal engine 4) have visual code (blueprints) (similar (like) scripting), before, i have 4 years of scripting experience in other game ArmA II, just the current task goes out of bound blueprints, need use c++,to expand the capabilities of blueprints – hekut146 Feb 04 '21 at 00:13

1 Answers1

0

how i can get speed class member by string?

You generally cannot. There is no mechanism in the language to achieve this. In other words, the C++ language doesn't have sufficient introspection features. You cannot access things based on the names of variables using runtime strings because those names aren't stored anywhere in the program. The names are just used for compilation and have no meaning when the program runs.

You could create an associative map data structure, which maps a string to a pointer to data member. The standard library provides containers that implements such maps: std::map and std::unordered_map:

using base_car = configFile::cfgVehicles::base_car;
std::unordered_map<std::string, int base_car::*> mapping {
    {"speed", &base_car::speed},
};

Now you can access the member through that map:

std::string whatGet = "speed";
int getInt = ptrDataObj->*mapping.find(whatGet)->second;

Is this a good design? Probably not. Perhaps a better design could be to just have a map from string to int and no classes at all.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks, so I can't use class members undeclared in the map previously? – hekut146 Feb 03 '21 at 23:06
  • @hekut146 I don't understand what you're aksing. – eerorika Feb 03 '21 at 23:06
  • Based on this code, I can only get the described members classes in the map? So it turns out? – hekut146 Feb 03 '21 at 23:12
  • 1
    @hekut146 The members that have been declared are the only members that exist. There are no members if the members haven't been declared. You can put all the members in the map. If you don't put something into the map, then it isn't in the map and cannot be found form it. – eerorika Feb 03 '21 at 23:12
  • Excuse me, another question: is it possible to use data_type by string? 2. std::string whereGet = "configFile::cfgVehicles::uazVehicle2"; 3.using uazVehicle2 = *whereGet; – hekut146 Feb 03 '21 at 23:30
  • @hekut146 I've modified the example to use the `whatGet` variable corresponding to the example in your question. Hope that clears it up. – eerorika Feb 03 '21 at 23:33
  • @hekut146 no, you can't define a `using` statement to point at a `string` like that. – Remy Lebeau Feb 03 '21 at 23:36
  • @eerorika thanks, I understood how to do (*what get*, based you example), I wanted to try to add the ability to specify *where* to get (I do not know what it is called, the dynamic data type by a string?),is it possible? the only thing (information) that i was found - cast (but cast don't support by string...) – hekut146 Feb 03 '21 at 23:43
  • You might want to use string_view instead of strings in the map – Yamahari Feb 04 '21 at 00:31