たとえば、どこかからデータを取得し、それを文字列変数に入れ、その内部のデータを別の文字列名として使用したいとします。
int main(void){
string strVar ="StringData"; //this is a string variable with text inside
cout<<strVar<<endl; //displaying the variables contents
string strVar.c_str() = "stuff in string variable 'StringData'"; //this uses what was inside of strVar to be the name of the new string variable
cout<<StringData<<endl; //prints the contents of the variable StringData (who got its name from the data inside of strVar
}
//OUTPUT:
StringData
stuff in string variable 'StringData'
この方法では絶対にできないことはわかっています。この例では、変数 StringData を使用する前に strVar の内容を事前に知っておく必要がありますが、理論的にはこれを行うことができますか?
編集:
皆さんありがとうございます。基本的には不可能です。C++ は動的変数言語ではなく、最も近いのはマップ (文字列、文字列) を使用することです。