如何從其他變量的值在C++中 印刷可變我是在C只是新++。C++:從變量值作爲變量
在PHP中,我們可以使/其他變量的值打印變量。 是這樣的。
$example = 'foo';
$foo = 'abc';
echo ${$example}; // the output will 'abc'
我該如何在C++中解決這個問題?
如何從其他變量的值在C++中 印刷可變我是在C只是新++。C++:從變量值作爲變量
在PHP中,我們可以使/其他變量的值打印變量。 是這樣的。
$example = 'foo';
$foo = 'abc';
echo ${$example}; // the output will 'abc'
我該如何在C++中解決這個問題?
看着它的另一種方式,它只是間接,其中C++廣泛使用。在C++中的類似的例子可能是...
using namespace std;
string foo = "abc";
string* example = &foo;
cout << *example << endl; // The output will 'abc'
...或使用參考而不是指針...
using namespace std;
string foo = "abc";
string& example = foo;
cout << example << endl; // The output will 'abc'
入門通過其名稱的變量/部件被稱爲反射/內省。
有一個在C++中沒有反射機制,基本上你不能這樣做。
你不能。
只有這樣,才能模擬這種(好樣的)是使用map
C++不具備該功能(這是反射): http://stackoverflow.com/questions/2911442/access-variable-value-using-string-representing-variables-name-in-c – birryree
指針....... –