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

  1. Path: sparky!uunet!olivea!spool.mu.edu!sdd.hp.com!apollo.hp.com!netnews
  2. From: vinoski@ch.apollo.hp.com (Stephen Vinoski)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is this CFRONT 3.0 warning message trying to tell me?
  5. Message-ID: <BzBzL2.5rx@apollo.hp.com>
  6. Date: 16 Dec 92 02:43:50 GMT
  7. References: <1992Dec15.183649.20994@delfin.com>
  8. Sender: usenet@apollo.hp.com (Usenet News)
  9. Organization: Hewlett-Packard Corporation, Chelmsford, MA
  10. Lines: 105
  11. Nntp-Posting-Host: srv.ch.apollo.hp.com
  12.  
  13. In article <1992Dec15.183649.20994@delfin.com> mark@delfin.com writes:
  14. >I'm porting a large application from a CFRONT 2.1-based compiler (Sun) to
  15. >a CFRONT 3.0-based compiler (Solbourne). The following warning message
  16. >has me somewhat mystified:
  17. >
  18. >"main.cc", line 31: warning: temporary used for non-const Thing& argument; no changes will be propagated to actual argument (anachronism)
  19. >
  20. >I've boiled the offending code down to:
  21. >
  22. >#include <string.h>
  23. >#include <malloc.h>
  24. >#include <stdio.h>
  25. >class Thing
  26. >{
  27. >   public:
  28. >
  29. >   char *str;
  30. >   Thing(){ str = (char *)0; };
  31. >   Thing(char *s){str=strdup(s);};
  32. >   Thing &operator = ( Thing & rhs  );
  33. >};
  34. >
  35. >Thing &
  36. >Thing::operator = ( Thing & rhs )
  37. >{
  38. >   if( this->str )free( this->str );
  39. >   strcpy( this->str, rhs.str );
  40. >   return *this;
  41. >}
  42. >
  43. >Thing getThing()
  44. >{
  45. >   Thing t("a thing");
  46. >   return t;
  47. >}
  48. >
  49. >main()
  50. >{
  51. >   Thing t1("");
  52. >   t1 = getThing();             // warning
  53. >   printf("value: %s\n", t1.str );
  54. >
  55. >   Thing t2 = getThing();       // no warning
  56. >   printf("value: %s\n", t2.str );
  57. >}
  58.  
  59. The warning is mainly caused by the non-const by-reference argument to
  60. Thing::operator=().  For this case, the compiler is generating code
  61. similar to this:
  62.  
  63.     Thing temp = getThing();
  64.     t1.operator=(temp);
  65.  
  66. The warning is effectively telling you that any changes that
  67. Thing::operator=() makes to its argument will not affect the value of
  68. the temporary object returned by getThing().
  69.  
  70. The no warning case is a constructor.  The code
  71.  
  72.     Thing t2 = getThing();
  73.  
  74. is in this case exactly equivalent to this invocation of the copy
  75. constructor:
  76.  
  77.     Thing t2(getThing());
  78.  
  79. In other words, Thing::operator=() is not invoked.  See the ARM,
  80. section 12.6.1, page 284.
  81.  
  82. BTW, there are problems with your assignment operator.  What if a
  83. Thing object is assigned to itself?
  84.  
  85.     Thing t1("oops");
  86.     t1 = t1;
  87.  
  88. Your assignment operator first frees any memory pointed at by the
  89. Thing::str data member, then copies directly into that freed memory.
  90. For self-assignment, it is also copying from that same freed memory.
  91.  
  92. To fix all of these problems, you should reimplement the assignment
  93. operator as follows:
  94.  
  95.     Thing &
  96.     Thing::operator=(
  97.         const Thing &rhs    // note the addition of const here
  98.     )
  99.     {
  100.         if (this != &rhs) {
  101.         if (str != 0) {
  102.             free(str);
  103.         }
  104.         s = strdup(rhs.str);
  105.         }
  106.         return *this;
  107.     }
  108.  
  109. I used free() and strdup() since that is what you used in your char*
  110. constructor, but you could also use new and delete, as you probably
  111. know.
  112.  
  113. -steve
  114.  
  115. Steve Vinoski  (508)436-5904   vinoski@apollo.hp.com
  116. Distributed Object Computing Program
  117. Hewlett-Packard, Chelmsford, MA 01824       These are my opinions.
  118.