CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2007
    Posts
    62

    Question A Variable variable name?

    Hi guys!
    I'm looking for a way to get something like this running: i want to pass to a function a argument whose name would be variable. A little more specific: there's this function
    Code:
    LoadSomething(int nvm, int hereWeAre)
    - it loads some graphics based on variable hereWeAre - basicly passing hereWeAre to the function would load graphics hereWeAre.bmp. Unfortunetaly I have hundreds of graphics to load so i want to do it in a loop - but here the problem appears: I dunno how to make the progam evaluate, lets say, a string variable saying hereWeAre2 so i can pass the value of the string as the name of the integer var needed in this function.
    Dunno if i made the problem clear enough. But hope someone can tell me if this is possible.
    Thanks,
    Sakurazuka
    Last edited by sakurazuka; September 20th, 2010 at 06:07 AM. Reason: [code]

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: A Variable variable name?

    Quote Originally Posted by sakurazuka View Post
    i want to pass to a function a argument whose name would be variable.
    Why don't you pass a String instead, like

    LoadSomething(int nvm, String loadName)

    Then you can do

    LoadSomething(someInt, "hereWeAre.bmp");

    If this isn't possible there's some additional condition you haven't revealed.

  3. #3
    Join Date
    Feb 2007
    Posts
    62

    Re: A Variable variable name?

    Well, the exact behavior I want to emulate would be something like this (sorry for the PHP style - but it shows exactly what i want/need to do - at least i think so)
    Code:
     
    $pref = hereWeAre
    $suf = 1
    $name = $pref+$suf
    LoadSomething(0, $($name))
    Quote Originally Posted by nuzzle View Post
    Why don't you pass a String instead, like

    LoadSomething(int nvm, String loadName)

    Then you can do

    LoadSomething(someInt, "hereWeAre.bmp");

    If this isn't possible there's some additional condition you haven't revealed.
    Unfortunetaly - it doesn't work that way - tried that. I think it's because each graphic file has a distinct integer attached to it - and an external integer variable is being created for the file. So i have to pass the integer variable hereWeAre as a parameter (corresponding to the file hereWeAre.bmp) - the value of which i don't know .

    I tried to solve this problem with some #define macros - but seem to be stuck at a point where the macro does not switch to integer:

    Code:
     #define replace( n )  ("_0_0" #n "_Bitmap")
    int iFrame = 1;
    LoadSomething(0, replace( iFrame ));
    the result of replace( iFrame )) is always
    Code:
     _0_0iFrame_Bitmap
    instead of
    Code:
     _0_01_Bitmap

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A Variable variable name?

    Preprocessor macros run before your program is even compiled, so it cannot possibly read variable contents. Besides, macros are evil. Just ask/look for the correct way to do string concatenation in c++:

    You can use a stringstream to stream all the variables into a string, and use that:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    void load_something(const std::string& bmp_name, int bmp_number)
    {
        std::ostringstream oss;
        oss << bmp_name << "-" << bmp_number << ".bmp";
    
        std::string file_name = oss.str();
        //do_it(file_name.c_str());
    
        std::cout << file_name << std::endl;
    }
    
    int main()
    {
        load_something("my_image", 5);
    }
    This print out "my_image-5.bmp" on my machine.

    The idea is that once the stream is full, you can extract a string from it, and from there, you can do whatever you want with it, or even its underlying c-style char array: c_str.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: A Variable variable name?

    Yes use strings or put a large switch statement in the LoadSomething function..
    But seriously, just learn how to use strings.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: A Variable variable name?

    You could use a std::map<int, std::string>.
    Last edited by ninja9578; September 20th, 2010 at 09:32 AM.

  7. #7
    Join Date
    Feb 2007
    Posts
    62

    Re: A Variable variable name?

    Well, as i checked the ostringstream and string constructs, and i guess the templates as well, are not supported on the architecture I'm writing at. I guess that only leaves me with the macros. I found a promising example but it doesn't do the things it's suppose to. Here's the link
    http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx
    It works as I stated earlier. Dunno why, though

  8. #8
    Join Date
    Feb 2007
    Posts
    62

    Re: A Variable variable name?

    Quote Originally Posted by sakurazuka View Post
    Dunno why, though
    Ok, nevermind - as monarch_dodra wrote before, it's not interpreted in the program but earlier. In the example they use a const value 9 as a parameter. It can't possibly work the way I wanted it to. **** it, I don't see a way out of this except the tiresome switch part :/. Blast - anyone got any ideas?

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A Variable variable name?

    As I said, forget macros. If you want to convert a number to a char array, you can use the not so standard but widely supported itoa function.

    BTW, all architectures support templates. You are writing plain old C, or you are using a crap compiler. Either way, I would look into my compiler's doc to see if it provides any kind of string like object.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: A Variable variable name?

    Quote Originally Posted by monarch_dodra
    If you want to convert a number to a char array, you can use the not so standard but widely supported itoa function.
    sprintf and snprintf are alternatives from the C standard library, and have been borrowed by the C++ standard library.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Feb 2007
    Posts
    62

    Re: A Variable variable name?

    Quote Originally Posted by monarch_dodra View Post
    or you are using a crap compiler.
    Nah man, it's just a homebrew compiler for a handheld. Maybe that makes it crappy but it's the only one there is. Anyway, I just wrote a bash script to edit the source file and create a really huge switch statement. Esthetically kinda sucks, but hey - it does the job...

  12. #12
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: A Variable variable name?

    Quote Originally Posted by sakurazuka View Post
    Well, as i checked the ostringstream and string constructs, and i guess the templates as well, are not supported on the architecture I'm writing at. I guess that only leaves me with the macros. I found a promising example but it doesn't do the things it's suppose to. Here's the link
    http://msdn.microsoft.com/en-us/libr...=VS.80%29.aspx
    It works as I stated earlier. Dunno why, though
    Looks like you got a wrong footing and digging at all the wrong places.
    Variable variables in PHP is a dynamic binding. Pointers drive the dynamic bindings in C++ so that's where you will likely find the answer not in the stringstream/macro or anything thereof. Once you get into it, you will see why C++ doesn't support - or why it is redundant - some of the things offered in PHP like string inter-polaraity or mainly variable variables.

    Bottom line:
    For starters, go with what ninja9578 said.

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