home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18369 < prev    next >
Encoding:
Text File  |  1992-12-23  |  2.9 KB  |  75 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!wingnut!pauljo
  3. From: pauljo@microsoft.com (Paul Johns)
  4. Subject: Re: Can't I initialize a dynamically allocated object in its constructor?
  5. Message-ID: <1992Dec23.202751.29726@microsoft.com>
  6. Date: 23 Dec 92 20:27:51 GMT
  7. Organization: Microsoft Corp.:  Redmond, Washington, USA
  8. References: <1992Dec22.234420.6610@microsoft.com> <1992Dec17.205113.1180@IRO.UMontreal.CA> 
  9. Lines: 64
  10.  
  11. I forgot to change the distribution to world in my last post, so it
  12. never got to the person who was asking.  Mea culpa.
  13.  
  14. I'm enclosing it verbatim below.  Mea culpa for the repeat.
  15.  
  16. I like the points the others have made, too!
  17.  
  18. // Paul Johns
  19.  
  20. In article <1992Dec22.234420.6610@microsoft.com> pauljo@microsoft.com wrote:
  21. > You've made common beginning mistake #1:  not defining
  22. > your own copy constructor when one is needed.
  23. > Your code WILL NOT WORK correctly until you provide a proper
  24. > copy constructor.  The fact that it seems to work in one case 
  25. > is not the whole story:  it is actually failing in subtle 
  26. > ways you're not looking at.  And the initialization you ask
  27. > about is not the issue here.
  28. > What's going on is that since you didn't provide a copy
  29. > constructor, the compiler provides one for you which simply
  30. > copies the members of your class one member at a time.
  31. > But the data in the vector doesn't get copied--only the pointer.  
  32. > And you end up deleting data twice since two different objects 
  33. > point to it.
  34. > This is really bad--it tends to mess up the heap something awful.
  35. > There is at least one C++ book whose author tells nothing
  36. > of copy constructors.  I hope you didn't use that one!!!
  37. > Good texts (Lippman, Stroustrup, various Microsoft Press books,
  38. > etc.) do a good job of explaining copy constructors and why 
  39. > they're needed.  Check out the dynamic string classes, which 
  40. > are very similar to your vector class.
  41. > If your class needs an assignment operator, it will almost
  42. > certainly need a copy constructor and destructor as well.
  43. > Two other points I noticed in your code:
  44. > 1.  You assignment operator doesn't check for doing "a = a;"  Also,
  45. >     it DOES make sense to assign vectors of different sizes,
  46. >     you may want to allow that.  Check how a good dynamic
  47. >     string class works for ideas.
  48. > 2.  operator + is usually implemented as a file-scope (global)
  49. >     function so that both operands can be converted, not
  50. >     just the RHS operand.  In your particular case, you
  51. >     don't have any useful (meaning you'd want to write
  52. >     something like "vecx = 5 + vecy;") conversions, so 
  53. >     this isn't an issue.  You may want to rethink the
  54. >     int constructor since it provides an implicit conversion
  55. >     from int to vector, meaning that an int or any expression
  56. >     that can be converted with a standard conversion to an
  57. >     int can be used any place a vector can.  (So "vec2 + 5"
  58. >     **IS** legal for your class, although meaningless.)
  59. > Good luck!
  60. > // Paul Johns
  61.  
  62.  
  63.