【问题标题】:Getting the name of a class in C++ [duplicate]在C ++中获取类的名称[重复]
【发布时间】:2016-01-04 13:41:12
【问题描述】:

在我的print 方法中,我想打印我的团队类的名称(“Bar”)。 有没有办法在不将其作为参数发送的情况下获取类的名称? 我想在 ostream cout

int main ()
{
    cout << "hello"<<endl;
    Team Bar;

    for (int i = 0; i < 2; i++)
    {
        Bar.addPlayer();
    }

    cout << Bar; //here I want to print the name of the Team ("Bar") without having a string Bar as a parameter

    Bar.manage();
    cout << Bar;

    return 0;
}

【问题讨论】:

  • 其实要打印变量名吗?还请包括 Team 类的定义。 Bar 只是类的一个实例,也就是一个变量。您的问题的答案可能在于here
  • 嗯,很奇怪,我在关闭投票中列出了两个重复项,但在问题关闭时只出现了一个。反正第二个是stackoverflow.com/questions/3649278/…
  • @Singer,您如何为 两个 重复投票?
  • @Lightness,好吧,我确定我之前尝试过列出用逗号分隔的 URL,它似乎有效。
  • @SingerOfTheFall:真的吗?我必须试试! (不过似乎不太可能……)

标签: c++ classname


【解决方案1】:

没有。这与类无关:您不能在运行时打印 any 变量的名称。这些名称在您的代码编译后不再存在。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 2021-11-07
  • 2017-08-22
相关资源
最近更新 更多