Reputation: 436
how to print variable from value of other variable in c++ i'm just new in c++.
in php we can make/print a variable by the value of other variable. like this.
$example = 'foo';
$foo = 'abc';
echo ${$example}; // the output will 'abc'
how can i solve this in c++?
Upvotes: 0
Views: 109
Reputation: 2987
Looking at it another way, it's just indirection, which C++ uses extensively. An analogous example in C++ might be...
using namespace std;
string foo = "abc";
string* example = &foo;
cout << *example << endl; // The output will 'abc'
...or using a reference instead of a pointer...
using namespace std;
string foo = "abc";
string& example = foo;
cout << example << endl; // The output will 'abc'
Upvotes: 0
Reputation: 59997
You cannot.
The only way to emulate this (well sort of) is to use a map
Upvotes: 3
Reputation: 137282
Getting a variable/member by its name is called reflection/introspection.
There is no reflection mechanism in C++, and basically you can't do that.
Upvotes: 1