#include<iostream>
using namespace std;
class Circle
{
public:
Circle(int x,int y,int r)
{
this->x=x;
this->y=y;
this->r=r;
}
public:
string GetName()
{
string ret;
//這個里面怎么填
return ret;
}
private:
int x,y;
int r;
};
int main()
{
Circle a(0,0,1);
cout <<a.GetName()<< endl;//希望輸出"a\n"
return 0;
}
uj5u.com熱心網友回復:
這個有難度吧,變數編譯成機器嗎后就是記憶體(偏移)地址,變數的名字只是代碼層次的。如果非要做,就在類里加個變數名成員,建構式里加個變數名引數,創建物件的時候把名字也一起保存了。
uj5u.com熱心網友回復:
從物件指標獲取類名:https://www.2cto.com/kf/201204/129622.html
uj5u.com熱心網友回復:
那么費勁嗎?C語言力一個宏定義不就搞定了#define NAME(a) #a
int a = 10;
printf(NAME(a));
uj5u.com熱心網友回復:
rtti研究下uj5u.com熱心網友回復:
做不到uj5u.com熱心網友回復:
class Circle
{
public:
Circle(int x,int y,int r,const string& name)
{
this->x=x;
this->y=y;
this->r=r;
this->ret = name;
}
public:
string GetName()
{
return ret;
}
private:
int x,y;
int r;
const string ret;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/131828.html
標籤:C++ 語言
上一篇:C語言
