2

I am wondering if there is any way to check if a given class contains a given member, except that given member's name is supplied as an std::string. Here is an example of the situation I am in:

class MyClass {
public:
    int a;
    int b;
    int c;
    void Handle_Class(std::string prop) {
        if (this->prop) { //pseudo code of what I want to accomplish
            //do stuff
        };
    };
} my_class;

int main() {
    my_class.Handle_Class("a");
};

I know Handle_Class is not the best name for a function like this, but it is basically supposed to check if the class has a member called prop. I have no idea how to actually do this though. Essentially I am trying to dynamically check if a class has a given member. I have a lot of experience in Lua, and you could easily accomplish this in Lua (although Lua is not object oriented, you could accomplish this using tables)

local my_table = {a = 123; b = 456; c = 789};
local function Handle_Table(prop)
    if my_table[prop] then
        print("property "..prop.." exists inside my_table!");
    end
end

I would appreciate any help. Thanks

iehrlich
  • 3,572
  • 4
  • 34
  • 43
mike bayko
  • 375
  • 5
  • 20
  • are u verifying some variables? – Megha Jul 07 '17 at 20:28
  • 1
    If I undestand well your approach this could be done with maps, you can set a couple of values for example a map like so you can compare the string and get the int. – Genaro Morales Jul 07 '17 at 20:30
  • 3
    See [Why does C++ not have reflection?](https://stackoverflow.com/q/359237/10077) – Fred Larson Jul 07 '17 at 20:31
  • No. It's not possible. – Jesper Juhl Jul 07 '17 at 20:35
  • 1
    Since c++ is a strongly typed class and is compiled, it won't actually let you do that. All the variable names get compiled down to memory locations that don't keep their name. Because of this, the code doesn't actually know the names of the variables, only that there is a number or string or whatever. Lua is a scripting language that doesn't get compiled. This means that the environment knows what the variables and properties are called. Very similar to the way javascript does it. – Chad K Jul 07 '17 at 21:00
  • Thank you guys for all of the help. – mike bayko Jul 07 '17 at 21:04
  • 1
    This also has nothing to do with object orientation. Also, if you were to store your variables into a map like Genaro Morales suggested, you could do that. And you would find something like that using if(map.find(prop) != map.end()) – Chad K Jul 07 '17 at 21:04
  • I believe that you are talking about **introspection**, or **reflection**. See this post: https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application – kmiklas Jul 07 '17 at 23:57
  • You could compile your application with debug symbols and try to [access these during runtime](https://stackoverflow.com/questions/3772742/is-there-a-way-to-access-debug-symbols-at-run-time). Otherwise, names and types just cease to exist after compilation. – Henri Menke Jul 08 '17 at 00:40

1 Answers1

0

There is also a means of doing this at compile-time with templates, you cannot specify a dynamic string at runtime for the check, but consider:

How to detect whether there is a specific member variable in class?

jschmerge
  • 320
  • 1
  • 4