我想將對象名稱保存爲字符串。我可以使用幾行代碼來解釋嗎?如何將對象名稱保存爲字符串
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class Example
{
public:
string object_name;
//code...
};
int main()
{
Example object;
cout<<object.object_name<<endl; //In this case the output should be "object", how to achieve this ?
return 0;
}
你將它設置爲:'object.object_name =「object」;' - 我很確定這不是你想要的答案,但C++沒有那種反射/內省。 –
[在運行時獲取C++對象名稱]的可能重複(http://stackoverflow.com/questions/468956/get-c-object-name-in-run-time) –
也許看看[C++ reflexion]( http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-ac-application) – steiner