0

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++?

1

3 Answers 3

3

You cannot.

The only way to emulate this (well sort of) is to use a map

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.