CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    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.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    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
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  5. #5
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206
    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.

  6. #6
    Join Date
    Mar 2003
    Location
    Germany, K-Town
    Posts
    578
    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

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640
    What about:
    Code:
    if (myMap["someKey"] == "someVal")
      ...
    else
      ...
    Doesn't this have the side effect of adding "someKey" to the map as well?

    Viggy

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640
    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.

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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
  •  





Click Here to Expand Forum to Full Width

Featured