I need to name vars on the fly. example
#include %26lt;iostream%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;string.h%26gt;
using namespace std;
int main()
{
string str_name = "bill";
string str_name;
// I want a string named bill , example : string bill
cout%26lt;%26lt;bill%26lt;%26lt;endl;
system("pause")
return 0;
}
In C++, how can I name vars , dynamicly?
You can't. Variable names are a convenience when referencing a particular variable. At compile time, they are transfered to memory locations which are integers. At runtime, there are no variable names, only numbers pointing to the memory address where the data is. If you need multiple variables of a certain type, us an array. If you need name / value pairs, use a hashtable.
Reply::-)) not sure whats the purpose here. Not sure what you trying to do is not possible. Variable (str_name) name is human readable name for the memory segment. "Bill" is the Value that memory location Contains.
try structures /class with array
struct x {
string name;
string value;
} X
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment