0

Object names? [C++]

Is it possible to get the name of an object? like, if I make a human class, then create an object called Daniel, like this, would I be able to get and output the name of the object? Human Daniel; Would I be able to get and display "Daniel"? I've just been setting a string as the name and using that to display the "name" for each individual object. Like this: Human Daniel("Daniel"); //Daniel being the string used to identify this specific object. I really only use these identifiers for testing, but it'd be great to just have a method for displaying the object name rather than a string I set.

14th Jan 2019, 12:33 AM
Daniel Cooper
Daniel Cooper - avatar
1 Respuesta
+ 3
There is no way one can do so, as the name is not bound to the instance you create. Consider for example a class Example: class Example{}; In main, you do: Example E1, &E2=E1; Now, E1 and E2(reference) points to the same address. That is why the name is not saved, as technically they point to the same address, and maintaining different names renders them different from each other. Thus, we are bound to continue using a string for storing the name. For more information, you can visit the following links: https://stackoverflow.com/questions/6927836/ways-to-use-variable-as-object-name-in-c-c https://stackoverflow.com/questions/468956/get-c-object-name-in-run-time
14th Jan 2019, 2:17 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar