Sign in to follow this  
Matthew Shockley

Index Struct by String

Recommended Posts

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?

Share this post


Link to post
Share on other sites
jpetrie    13159
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).

Share this post


Link to post
Share on other sites
jpetrie    13159
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.

Share this post


Link to post
Share on other sites
Ravyne    14300
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this