|
-
September 20th, 2010, 06:05 AM
#1
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]
-
September 20th, 2010, 06:38 AM
#2
Re: A Variable variable name?
 Originally Posted by sakurazuka
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.
-
September 20th, 2010, 07:54 AM
#3
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))
 Originally Posted by nuzzle
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
instead of
-
September 20th, 2010, 08:15 AM
#4
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.
-
September 20th, 2010, 08:39 AM
#5
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.
-
September 20th, 2010, 09:27 AM
#6
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.
-
September 20th, 2010, 01:34 PM
#7
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
-
September 20th, 2010, 01:40 PM
#8
Re: A Variable variable name?
 Originally Posted by sakurazuka
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?
-
September 20th, 2010, 02:16 PM
#9
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.
-
September 20th, 2010, 02:39 PM
#10
Re: A Variable variable name?
 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.
-
September 20th, 2010, 02:39 PM
#11
Re: A Variable variable name?
 Originally Posted by monarch_dodra
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...
-
September 20th, 2010, 10:07 PM
#12
Re: A Variable variable name?
 Originally Posted by sakurazuka
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|