【发布时间】:2020-12-11 23:36:19
【问题描述】:
免责声明:我对编程还很陌生,我的术语可能还很陌生。当我说“类指针名称”时,我指的是“myClass A”和“A.Fill(...)”中的“A”。 我不确定如何表述我的问题以使其尽可能清晰,但让我试试吧:
class myClass{
public:
// stuff
Fill(string msg)
{
// does stuff
cout << msg + "some extra text"; /*here I want to somehow get the name of the class pointer
which was used when calling this method, rather than having to manually pass it on as a string.*/
// more stuff
}
};
int main()
{
myClass A;
myClass B;
myClass C;
A.Fill("A");
B.Fill("B");
C.Fill("C");
}
这段代码目前正在做我想要的。但是,我想知道是否有可能以某种方式从方法中获取类指针名称,而不必每次都手动传递字符串参数? 我知道在宏中我可以使用 #A #B #C 来准确地做到这一点,但我不确定这将如何应用于我的代码,以及是否可能。
【问题讨论】:
-
不,您不能从类函数中获取变量名。它们不会被保留以供在运行时使用。