home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20042 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.2 KB  |  92 lines

  1. Path: sparky!uunet!gumby!yale!yale.edu!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: name of object
  5. Date: 28 Jan 1993 20:58:22 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 80
  8. Message-ID: <1k9hdeINNjdq@early-bird.think.com>
  9. References: <g++FAQ_01_15_1993_texi@ohm.berkeley.edu> <g++FAQ_01_15_1993_plain@ohm.berkeley.edu> <1993Jan28.173602.5232@gmuvax2.gmu.edu>
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <1993Jan28.173602.5232@gmuvax2.gmu.edu> twallace@gmuvax2.gmu.edu (Todd Wallace) writes:
  13. >Here is an odd question for the OOP gurus:
  14. >Is there a way for an object to know its own name?
  15.  
  16. There's no built-in way in the language.  Some objects don't even have
  17. names.
  18.  
  19. >For instance, let's say we declare the following class:
  20. >class Lemon {
  21. >    public:
  22. >    //some stuff
  23. >};
  24. >
  25. >void main(void)
  26. >{
  27. >    Lemon A,B,C;
  28. >
  29. >    A.tell_me_your_name;
  30. >    B.tell_me_your_name;
  31. >    C.tell_me_your_name;
  32. >}
  33.  
  34. How's this:
  35.  
  36. class NamedThing {
  37.     char *name;
  38.     public:
  39.     void tell_me_your_name() {cout << name << '\n';};
  40.     NamedThing(char *initial_name) {name = initial_name;};
  41. };
  42.  
  43. class Lemon: virtual public NamedThing {
  44.     ...
  45. };
  46.  
  47. void main(void) {
  48.     Lemon A("A"), B("B"), C("C");
  49.     A.tell_me_your_name;
  50.     B.tell_me_your_name;
  51.     C.tell_me_your_name;
  52. }
  53.  
  54. If you don't like having to write the name twice, use a macro:
  55.  
  56. #define NAMED(name) name(#name)
  57.  
  58. Then you can write:
  59.  
  60.     Lemon NAMED(A), NAMED(B), NAMED(C);
  61.  
  62.     Lemon *foo_ptr = NAMED(foo);
  63.  
  64.  
  65.  
  66. >
  67. >and I would like the output to be:
  68. >A
  69. >B
  70. >C
  71. >
  72. >Therefore, I have to declare a member function for Lemon that
  73. >can look up its own name.
  74. >
  75. >Can it be done? It would help a lot with debugging.
  76. >
  77. >
  78. >----------------------------------------------------------------
  79. >|    Todd Wallace               |  "A pessimist is surprised   |
  80. >|    twallace@gmuvax2.gmu.edu   |   as often as an optimist,   |
  81. >|                               |   but always pleasantly."    |
  82. >|                               |            - Robert Heinlein |
  83. >----------------------------------------------------------------
  84. >
  85.  
  86.  
  87. -- 
  88. Barry Margolin
  89. System Manager, Thinking Machines Corp.
  90.  
  91. barmar@think.com          {uunet,harvard}!think!barmar
  92.