home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18159 < prev    next >
Encoding:
Text File  |  1992-12-18  |  2.1 KB  |  80 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!ux1.cso.uiuc.edu!cs.uiuc.edu!sparc0b!pjl
  3. From: pjl@cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: What is this CFRONT 3.0 warning message trying to tell me?
  5. Message-ID: <BzF8yn.29o@cs.uiuc.edu>
  6. Sender: news@cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1992Dec15.183649.20994@delfin.com>
  9. Distribution: usa
  10. Date: Thu, 17 Dec 1992 20:59:11 GMT
  11. Lines: 67
  12.  
  13. In <1992Dec15.183649.20994@delfin.com> mr@delfin.com (Mark Rosenberg) writes:
  14.  
  15. >I'm porting a large application from a CFRONT 2.1-based compiler (Sun) to
  16. >a CFRONT 3.0-based compiler (Solbourne). The following warning message
  17. >has me somewhat mystified:
  18.  
  19. >"main.cc", line 31: warning: temporary used for non-const Thing& argument; no changes will be propagated to actual argument (anachronism)
  20.  
  21. >I've boiled the offending code down to:
  22.  
  23. >#include <string.h>
  24. >#include <malloc.h>
  25. >#include <stdio.h>
  26. >class Thing
  27. >{
  28. >   public:
  29.  
  30. >   char *str;
  31. >   Thing(){ str = (char *)0; };
  32. >   Thing(char *s){str=strdup(s);};
  33. >   Thing &operator = ( Thing & rhs  );
  34. >};
  35.  
  36. *****>    Because this   ^^^^^^^^^^^^^ isn't const.
  37.  
  38.             Thing const &rhs
  39.  
  40. >Thing &
  41. >Thing::operator = ( Thing & rhs )
  42. >{
  43. >   if( this->str )free( this->str );
  44. >   strcpy( this->str, rhs.str );
  45. >   return *this;
  46. >}
  47.  
  48. >Thing getThing()
  49. >{
  50. >   Thing t("a thing");
  51. >   return t;
  52. >}
  53.  
  54. >main()
  55. >{
  56. >   Thing t1("");
  57. >   t1 = getThing();             // warning
  58. >   printf("value: %s\n", t1.str );
  59.  
  60. >   Thing t2 = getThing();       // no warning
  61. >   printf("value: %s\n", t2.str );
  62. >}
  63.  
  64. >When this program is run you get the expectable result:
  65.  
  66. >value: a thing
  67. >value: a thing
  68.  
  69. >I realize that getThing should really return a reference and that using
  70.  
  71. *****>    No it shouldn't; your program will die a horrible death if it does.
  72.     Thou shalt not return a reference to a local variable.
  73.  
  74. >stdio is really bad style but I don't understand why the first call to
  75. >getThing generates a warning but the second doesn't.
  76. -- 
  77.     - Paul J. Lucas                University of Illinois    
  78.       AT&T Bell Laboratories        at Urbana-Champaign
  79.       Naperville, IL            pjl@cs.uiuc.edu
  80.