Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 25-Apr-16 3:11am

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.
 
Share this answer
 
v2
Comments
R. S. Verma 25-Apr-16 9:20am    
Hey OriginalGriff,
Lots of thanks for clerifying my doubts.
OriginalGriff 25-Apr-16 9:37am    
You're welcome!
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.
 
Share this answer
 
Comments
R. S. Verma 25-Apr-16 9:25am    
Thanks jochen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900