home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18335 < prev    next >
Encoding:
Text File  |  1992-12-23  |  2.4 KB  |  64 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: <1992Dec22.234420.6610@microsoft.com>
  6. Date: 22 Dec 92 23:44:20 GMT
  7. Organization: Microsoft Corp.
  8. References: <1992Dec17.205113.1180@IRO.UMontreal.CA> 
  9. Distribution: usa
  10. Lines: 52
  11.  
  12. You've made common beginning mistake #1:  not defining
  13. your own copy constructor when one is needed.
  14.  
  15. Your code WILL NOT WORK correctly until you provide a proper
  16. copy constructor.  The fact that it seems to work in one case 
  17. is not the whole story:  it is actually failing in subtle 
  18. ways you're not looking at.  And the initialization you ask
  19. about is not the issue here.
  20.  
  21. What's going on is that since you didn't provide a copy
  22. constructor, the compiler provides one for you which simply
  23. copies the members of your class one member at a time.
  24.  
  25. But the data in the vector doesn't get copied--only the pointer.  
  26. And you end up deleting data twice since two different objects 
  27. point to it.
  28.  
  29. This is really bad--it tends to mess up the heap something awful.
  30.  
  31. There is at least one C++ book whose author tells nothing
  32. of copy constructors.  I hope you didn't use that one!!!
  33.  
  34. Good texts (Lippman, Stroustrup, various Microsoft Press books,
  35. etc.) do a good job of explaining copy constructors and why 
  36. they're needed.  Check out the dynamic string classes, which 
  37. are very similar to your vector class.
  38.  
  39. If your class needs an assignment operator, it will almost
  40. certainly need a copy constructor and destructor as well.
  41.  
  42. Two other points I noticed in your code:
  43.  
  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.  
  49. 2.  operator + is usually implemented as a file-scope (global)
  50.     function so that both operands can be converted, not
  51.     just the RHS operand.  In your particular case, you
  52.     don't have any useful (meaning you'd want to write
  53.     something like "vecx = 5 + vecy;") conversions, so 
  54.     this isn't an issue.  You may want to rethink the
  55.     int constructor since it provides an implicit conversion
  56.     from int to vector, meaning that an int or any expression
  57.     that can be converted with a standard conversion to an
  58.     int can be used any place a vector can.  (So "vec2 + 5"
  59.     **IS** legal for your class, although meaningless.)
  60.  
  61. Good luck!
  62.  
  63. // Paul Johns
  64.