|
-
June 17th, 2004, 09:00 AM
#1
Virtual Variable Naming?
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.
-
June 17th, 2004, 09:54 AM
#2
Short answer: no.
I'm finding it difficult to come up with a sensible use for such an ability.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
June 17th, 2004, 12:07 PM
#3
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;
PHP Code:
MyVarData["Dog"] = "Rover";
MyVarData["Cat"] = "Puffy";
MyVarData["Mouse"] = "LongTooth";
std::cout << "My cat's name is " << MyVarData["Cat"] << std::endl;
-
June 17th, 2004, 12:16 PM
#4
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".....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
June 17th, 2004, 02:04 PM
#5
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.
-
June 18th, 2004, 01:00 AM
#6
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:
Code:
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).
- Matthias
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." - Bjarne Stroustrup
-
June 18th, 2004, 02:45 AM
#7
Originally posted by matthias_k
Is this the common procedure to access data in a std::map? I always did it like this:
Code:
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... 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...
-
June 18th, 2004, 10:26 AM
#8
What about:
Code:
if (myMap["someKey"] == "someVal")
...
else
...
Doesn't this have the side effect of adding "someKey" to the map as well?
Viggy
-
June 18th, 2004, 10:44 AM
#9
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...
-
June 18th, 2004, 10:48 AM
#10
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.
-
June 18th, 2004, 10:52 AM
#11
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|