如何获得 C++ 运行时的对象名称?

发布于 2024-07-13 08:50:02 字数 59 浏览 4 评论 0原文

我可以在运行时获取对象的名称(例如通过 RTTI 获取对象的类型)吗? 我希望该对象能够打印它的名称。

Can I get an object's name in run time (like getting an object's type via RTTI)? I want the object to be able to print its name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

神爱温柔 2024-07-20 08:50:02

由于 C++ 中的对象没有任何名称,因此您无法获取它们。 唯一可以识别对象的就是它的地址。

否则,您可以实现您的命名方案(这意味着对象将具有一些 char*std::string 成员及其名称)。 您可以通过 Qt 的 QObject 层次结构来激发自己的灵感,它使用类似的方法。

Since objects in C++ don't have any names, you cannot get them. The only thing you can get to identify an object is its address.

Otherwise, you can implement your naming scheme (which means the objects would have some char* or std::string member with their name). You can inspire yourself in Qt with their QObject hierarchy, which uses a similar approach.

或十年 2024-07-20 08:50:02

这是不可能的。 事实上,一个对象没有唯一的名称。

A a;
A& ar = a;  // both a and ar refer to the same object

new A;  // the object created doesn't have a name

A* ap = new A[100];  // either all 100 objects share the same name, or need to 
                     // know that they are part of an array.

最好的办法是向对象构造函数添加一个字符串参数,并在创建时为其指定一个名称。

Its not possible. For on thing, an object doesn't have a unique name.

A a;
A& ar = a;  // both a and ar refer to the same object

new A;  // the object created doesn't have a name

A* ap = new A[100];  // either all 100 objects share the same name, or need to 
                     // know that they are part of an array.

Your best bet is to add a string argument to the objects constructor, and give it a name when its created.

<逆流佳人身旁 2024-07-20 08:50:02

该语言不允许您访问该信息。
编译代码时,所有命名对象都已转换为相对内存位置。 甚至这些位置由于优化而重叠(即一旦一个变量不再使用,它​​的空间就可以被另一个变量使用)。

您需要的信息存储在大多数编译器生成的调试符号中,但这些信息通常从可执行文件的发行版本中删除,因此您不能保证它们存在。

即使调试符号存在,它们都是特定于编译器/平台的,因此您的代码不能在操作系统甚至同一操作系统上的编译器之间移植。 如果您确实想学习本课程,您需要阅读并了解您平台的调试器如何工作(除非您已经编写了编译器,这非常重要)。

The language does not give you access to that information.
By the time the code has been compiled all named objects have been translated into relative memory locations. And even these locations overlap because of optimization (ie once a variable is no longer in use its space can be used by another variable).

The information you need is stored in the debug symbols that are generated by most compilers but these are usually stripped from release versions of the executable so you can not guarantee they exist.

Even if the debug symbols existed they are all compiler/platform specfic so your code would not be portable between OS or even compilers on the same OS. If you really want to follow this course you need to read and understand how the debugger for your platform works (unless you have already written a compiler this is very non trivial).

山人契 2024-07-20 08:50:02

这可能是 GCC 特有的:

#include <typeinfo>
#include <iostream>

template <typename T>
void foo(T t)
{
    std::cout << typeid(t).name() << std::endl;
}

This may be GCC-specific:

#include <typeinfo>
#include <iostream>

template <typename T>
void foo(T t)
{
    std::cout << typeid(t).name() << std::endl;
}
初相遇 2024-07-20 08:50:02

C++ 并不真正支持反射。 然而,谷歌搜索产生了一些替代方法,我怀疑你但会发现它们很有用。

C++ doesn't really support reflection. However, a bit of googling produced a couple of alternate methods, I doubt you will find them useful though.

混吃等死 2024-07-20 08:50:02

C++ 对象没有“名称”(除非我错误地理解了问题),您最好的希望是在创建它们时为它们命名。

class NamedObject
{
  String name;
  NamedObject(String argname)
  { 
    name = argname;
  }
}

NamedObject phil("phil");

C++ objects don't have 'names' (unless I am understanding the problem wrong) Your best hope is to name them as you make them.

class NamedObject
{
  String name;
  NamedObject(String argname)
  { 
    name = argname;
  }
}

NamedObject phil("phil");
甜中书 2024-07-20 08:50:02

如果你的意思是变量的名称,我认为这是不可能的。 也许如果你使用 GNU 调试器选项进行编译...但即使这样,我也不认为该语言有这样做的结构。

If you mean the name of the variable, I don't think this is possible. Maybe if you compile with the GNU Debugger option on ... but even in that way I don't think the language have constructs to do that.

大姐,你呐 2024-07-20 08:50:02

所以,这基本上就是我所做的。 虽然很hacky,但确实有效。 我创建了一个利用字符串化的可变参数宏。 不幸的是,它变得笨拙,需要一个 _dummy 参数来提供伪默认 ctor,因为你不能省略分隔命名参数和变量参数的逗号(我什至尝试使用 gnu cpp,但不成功 - 可能我还不够努力)。

#include <string>

#define MyNamedClass( objname, ... ) MyClass objname(__VA_ARGS__, #objname )

class MyClass
{
public:
   MyClass( void* _dummy=nullptr, const std::string& _name="anonymous") : name( _name ) {}
   MyClass( int i, const std::string& _name="anonymous" ) : name( _name ) {}

private:
   std::string name;
};


int main()
{
   MyClass mc0;
   MyClass mc1(54321);
   MyNamedClass( mc2, nullptr);
   MyNamedClass( mc3, 12345 );

   return 0;
}

So, this is basically what I did. It's hacky, but it does the trick. I created a variadic macro that takes advantage of stringizing. Unfortunately, it becomes clumsy with the need for a _dummy parameter in order to provide the pseudo-default ctor, because you cannot omit the comma separating the named argument from the variable arguments (I even tried with gnu cpp, but was unsucessful--may I didn't try hard enough).

#include <string>

#define MyNamedClass( objname, ... ) MyClass objname(__VA_ARGS__, #objname )

class MyClass
{
public:
   MyClass( void* _dummy=nullptr, const std::string& _name="anonymous") : name( _name ) {}
   MyClass( int i, const std::string& _name="anonymous" ) : name( _name ) {}

private:
   std::string name;
};


int main()
{
   MyClass mc0;
   MyClass mc1(54321);
   MyNamedClass( mc2, nullptr);
   MyNamedClass( mc3, 12345 );

   return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文