code_junkie

get C++ object name in run time

Nov 13th, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. A a;
  2. A& ar = a; // both a and ar refer to the same object
  3.  
  4. new A; // the object created doesn't have a name
  5.  
  6. A* ap = new A[100]; // either all 100 objects share the same name, or need to
  7. // know that they are part of an array.
  8.  
  9. #include <typeinfo>
  10. #include <iostream>
  11.  
  12. template <typename T>
  13. void foo(T t)
  14. {
  15. std::cout << typeid(t).name() << std::endl;
  16. }
  17.  
  18. class NamedObject
  19. {
  20. String name;
  21. NamedObject(String argname)
  22. {
  23. name = argname;
  24. }
  25. }
  26.  
  27. NamedObject phil("phil");
Add Comment
Please, Sign In to add comment