Index Struct by String

Started by
4 comments, last by Ravyne 13 years, 11 months ago
Let's say I have a struct and when the user inputs a word, I want to see if the struct has that member variable. Is there a way to do it like this: if (structName[indStr]) {} or will I have to put all the members in a list and check them manually?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Advertisement
Language? Assuming C, no. Assuming C++, you can implement operator[] but you will have to "put all the members in a list and check them manually" (or similar manual check).
I mean in C++, sorry.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Then see above, but it doesn't really matter because you know the answer already -- you have the compile-time type of the object you are operating on, and if you are making the decision because of the possibility some as-yet-unknown subclass may optionally have a member, your design is bad and you should fix it.
I don't need it, but it would make life easier for me for the user based game I am making. I am so used to programming in Lua. Thanks, though.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
You're essentially asking for "Properties", I think one of the recent journal entries on the front page had some great information on that.

In general, properties usually are just named, and not associated with an actual member variable, but its very possible to have it both ways, though you'll have to either accept some limitations or accept that member variables and their 'properties' might diverge due to how the lookup rules are handled. I was musing about how to create a unified property system over lunch today, actually.

At any rate, yes its possible, but you have to make it work yourself. The naive way is to simply have a static std::map to each class which contains a mapping for property names to their offset relative to the base of the instance (the 'this' pointer), then you just impliment a function which takes in a property name, does the lookup, and casts to the proper type.

If you're insistant upon using the array index notation, you either have to settle on a single type, returning a base-type pointer that can be queried for the real type, or returning a void pointer (or any pointer type I suppose, but that's why we have void pointers in the first place) and doing the cast yourself. I wouldn't recommend any of these options.

Alternatively, you can get some amount of type safety with function signatures like 'GetFloatProperty(...)' or a templated function that would be used like "GetProperty<float>(...) -- which isn't safe on its own, but these functions could be implimented to check additional metadata about type that was set at initialization time, which at least tells you whether the type requested is consistant with the type it was initialized with.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement