Click to See Complete Forum and Search --> : Virtual Variable Naming?


MrDoomMaster
June 17th, 2004, 09:00 AM
I'm wondering if there is a feature available that will allow you to name variables in a loop based on an array of strings?

Of course this may seem pointless, but not on terms of organization. I could have this array of strings that will later represent my variable names in a header file, while in my CPP I have a nice, small, clean looking loop to name X number of variables.

For example, if I have the following array:

string VariableNames[] = {Cat, Dog, Mouse};

This would later on be put into a loop which creates a variable of a given type named Cat, Dog, and Mouse!

I know there are virtual functions that base their name reference on strings, so I was hoping this would be a possibility. Thanks.

Graham
June 17th, 2004, 09:54 AM
Short answer: no.

I'm finding it difficult to come up with a sensible use for such an ability.

Axter
June 17th, 2004, 12:07 PM
I'm not sure if this is what you're looking for, but you could use map<string, string> type to give you this functionallity.

std::map<std::string, std::string> MyVarData;


MyVarData["Dog"] = "Rover";
MyVarData["Cat"] = "Puffy";
MyVarData["Mouse"] = "LongTooth";

std::cout << "My cat's name is " << MyVarData["Cat"] << std::endl;

TheCPUWizard
June 17th, 2004, 12:16 PM
DoomMaster,

As posted by others, there is no direct way to do this using the C++ COMPILER. However, it can be done using the pre-processor.
there are pros and cons to doing this.

The result can be harder do debug, but if you have a large number or very dynamic situation, or complex declarations, it can provide a nice clean solution.

ps: I am USUALLY against the use of the pre-processor, but this is a powerful technique when used in moderation.

Also see the book "Modern C++ Design".....

MrDoomMaster
June 17th, 2004, 02:04 PM
Thanks for the help guys. By reading your replies, it seems I'll just forget it. I don't want to have to get too complicated about it. I was just questioning the possibilities.

matthias_k
June 18th, 2004, 01:00 AM
MyVarData["Dog"] = "Rover";
MyVarData["Cat"] = "Puffy";
MyVarData["Mouse"] = "LongTooth";

Is this the common procedure to access data in a std::map? I always did it like this:

std::map<int,std::string>::iterator pIter = my_map.find( index );
std::cout << pIter->second << std::endl;
Just wondering, because Axter's attempt is likely much better (and much more handy).

Andreas Masur
June 18th, 2004, 02:45 AM
Originally posted by matthias_k
Is this the common procedure to access data in a std::map? I always did it like this:

std::map<int,std::string>::iterator pIter = my_map.find( index );
std::cout << pIter->second << std::endl;
Just wondering, because Axter's attempt is likely much better (and much more handy).
Well...it is as common as yours... :cool: There are some things to consider like e.g. processing time of these statements. Furthermore, Axter's approach will add the item to the map if it is not there...while your approach will not add such an item...

MrViggy
June 18th, 2004, 10:26 AM
What about:

if (myMap["someKey"] == "someVal")
...
else
...

Doesn't this have the side effect of adding "someKey" to the map as well?

Viggy

Andreas Masur
June 18th, 2004, 10:44 AM
Originally posted by MrViggy
Doesn't this have the side effect of adding "someKey" to the map as well?

Yes...if 'someKey' did not exists before...

MrViggy
June 18th, 2004, 10:48 AM
Yeah, I remember now. That kinda burned me once. I had forgotten that this would add the key, if the key didn't exist.

Had some undesirable side effects in my app!

;)

Vig.

Andreas Masur
June 18th, 2004, 10:52 AM
Originally posted by MrViggy
Had some undesirable side effects in my app!
Yes...and these can result in hours of debugging...since they are hard to find... :cool: