Home > Back-end >  How to capture the object name in the class class definition?
How to capture the object name in the class class definition?

Time:10-21

#include
using namespace std;

The class Circle
{
Public:
Circle (int x, int y, int r)
{
This - & gt; X=x;
This - & gt; Y=y;
This - & gt; r=r;
}

Public:
String GetName ()
{
String ret.

//the inside how to fill out


return ret;
}

Private:
Int x, y;
int r;

};

Int main ()
{
Circle a (0, 1);
cout

return 0;
}

CodePudding user response:

This is difficult, variables are compiled into machine (offset) after it is memory address, the name of the variable only code level,
If you do, just add a member variable names in the class, add a variable name parameter constructor, create the object when the name is also saved together,

CodePudding user response:

From the object pointer to obtain the name of the class:
https://www.2cto.com/kf/201204/129622.html

CodePudding user response:

So difficult? C force a macro definition is not done
# # define NAME (a) a
int a=10;
Printf (NAME) (a);

CodePudding user response:

Rtti under study

CodePudding user response:

do

CodePudding user response:

 class Circle 
{
Public:
Circle (int x, int y, int r, const string& Name)
{
This - & gt; X=x;
This - & gt; Y=y;
This - & gt; r=r;
This - & gt; Ret=name;
}

Public:
String GetName ()
{

return ret;
}

Private:
Int x, y;
int r;
Const string ret.

};
  • Related