查找在堆上创建的对象名称 [英] Find object name which is created on heap

查看:93
本文介绍了查找在堆上创建的对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c ++中的新关键字找到在堆上创建的对象的名称?

假设我有一个类并在main中创建此类的对象。那么指针指向的物体名称是什么:



A类

{

A (){}



};

int main()

{

A * ptr = new A();

//这里对象将创建并分配内存,这是分配给指针ptr的
//所以我想知道名字

//通过A()创建的对象

返回0;

}



我尝试过:



我找不到任何方法来获取使用new关键字创建的对象名称。请提出我需要做些什么。

谢谢

how to find name of object which is created on heap using new keywords in c++?
Suppose i have a class and create object of this class in main. so what will be the name of object which is pointed by pointer:

class A
{
A(){}

};
int main()
{
A *ptr=new A();
//here object will create and allocate memory which is
//assigned to pointer ptr, so i would like to know name
//of the object created through A()
return 0;
}

What I have tried:

I dont find any way to get the name of object which is created using new keyword. please suggest what I need to do regarding this.
Thanks

推荐答案

它没有名字。它不需要一个,因为没有任何东西可以使用它 - 而是它有一个 引用 地址 存储在变量中,系统将使用该变量来识别将来的物体。



如果您愿意,它就像一张纸币:一张5英镑的纸币是就像所有其他的5英镑钞票一样,除了它有一个唯一的序列号(实际上是参考)。在正常情况下,您不必担心(甚至看起来!)序列号,因为它们都值得相同金额。序列号(引用)是这样的,以便官员(系统)可以在需要时唯一地标识注释(实例)。
It doesn't have a name. It doesn't need one, because nothing will ever use it - instead it has a reference or address which is stored in the variable and which is used by the system to identify the object in future.

If you like, it's like a currency note: a £5 note is just like all the other £5 notes, except it has a unique serial number (which is in effect the reference). Under normal circumstances you don't worry (or even look!) at the serial number as they are all worth the same amount of money. The serial number (reference) is there so that officials (the system) can uniquely identify the note (instance) if they need to.


在C ++中不支持对象类名称标准。但是大多数框架都提供了一个支持MFC等名称的对象类 CObject Class [ ^ ] //msdn.microsoft.com/en-us/library/a7t4z25b.aspx\">参考:: GetRuntimeClass [ ^ ]和Qt QObject类 [ ^ ]。



使用它们从这些对象类派生你的类。
There is no support for object class names in the C++ standard. But most frameworks provide an object class that supports names like the MFC CObject Class[^] with the member function CObject::GetRuntimeClass[^] and the Qt QObject Class[^].

To use them derive your class from those object classes.


这篇关于查找在堆上创建的对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆