How can i convert a vartiables value int anothers name in C++? like in this php snippet.
$string = 'testVar';
${$string} = 'test';
echo $testVar; // test
How can i convert a vartiables value int anothers name in C++? like in this php snippet.
$string = 'testVar';
${$string} = 'test';
echo $testVar; // test
How about using a map?
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string, string> hitters;
hitters["leadoff"] = "Jeter";
hitters["second"] = "Damon";
hitters["third"] = "Teixiera";
hitters["cleanup"] = "Matsui";
string hitter = "cleanup";
cout << hitters[hitter] << endl;
I don't know PHP, but I don't think you can do that in C++. A variable name has no runtime representation in a compiled C++ program, so there is no way to load its name at runtime like that.
This seems to be something you can only do in a scripting language where the original source code is in memory or at least some representation of the syntax tree.
This isn't something you can do in C++. When you compile C++ code, the information about what names functions and variables are is lost (technically, it can be stored in symbol tables, but those are only for debugging purposes). To accomplish anything like this, you'd need to use a map or other similar data structure, which is rather like a PHP array.
It sounds like you may want to use pointers:
string testVar;
string *str = &testVar;
*str = "test";
cout << testVar << endl; // test
After compiling, the C++ compiler discards information like the original names of variables, so you have to use lower level constructs to do the same kinds of things.
If you compile your program with a symbol table, you can use "readelf -s yourexecutable" (linux) to get the symbol table. Then grep on the output with the variable name and from the output get the address by using the"cut" command.
readelf -s a.out | grep ' var$' | tr -s ' '| cut -d' ' -f3