home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18185 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  2.0 KB

  1. Path: sparky!uunet!gatech!prism!federation!andy
  2. From: andy@federation.gatech.edu (Andy Register)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Can't I initialize a dynamically allocated object in its constructor?
  5. Message-ID: <78548@hydra.gatech.EDU>
  6. Date: 18 Dec 92 00:50:52 GMT
  7. References: <1992Dec17.205113.1180@IRO.UMontreal.CA>
  8. Sender: news@prism.gatech.EDU
  9. Organization: CERL-EE, Georgia Institue of Technology
  10. Lines: 54
  11.  
  12. In article <1992Dec17.205113.1180@IRO.UMontreal.CA> laviers@IRO.UMontreal.CA (Cocotte Minute) writes:
  13. >Hello,
  14. >
  15. >I have the following problem.
  16. >
  17. >a temporary vector is constructed to put the result of v1+v2 .
  18. >If there is no initialization in the constructor , it works just fine, 
  19. >if there is initialization (here, to [-1,-1,-1...]), we seem to loose the value 
  20. >of v1+v2... why???
  21. >
  22.  
  23. You are almost there, you have failed to include a copy constructor 
  24. that does a deep copy, i.e.
  25.  
  26. Vector::Vector(const Vector& v);
  27. You have to allocate memory, assign values, blah, blah blah.
  28.  
  29. Also, I like to have the following syntax for my operator=
  30.  
  31. const Vector& Vector::operator=(const Vector& v);
  32.  
  33. The first const means that folks cannot a=b=c=... and 
  34. screw up assignment.  Coplien or Meyers talks about this.  You can
  35. also return a reference and I think it speeds things up.
  36.  
  37. The second const and & emulate a fast pass by value.  You don't 
  38. have to create the temp to pass but you still cannot alter the
  39. passed in parameter (without heroic efforts on your part).
  40.  
  41.  
  42. >Pohl (and others) seem to think that it is good practice to initialize
  43. >objects... and have no undefined values.
  44.  
  45. Yes I think this is a good idea but you cannot do it generically with
  46. templates.
  47.   float DefaultValue() either is an undefined operation or
  48.       results in an undefined floating point number I forget which.
  49.  
  50. on the other hand
  51.   Vector DefaultValue() can be well defined.  
  52.  
  53. You can see this kind of thing when you use templates.  If anybody is 
  54. listening, am I correct on this point?
  55.  
  56. >is this a bug in the compiler? 
  57.  
  58. No.
  59.  
  60. >Thank you very much for any help you can give me!
  61.  
  62. welcome
  63.  
  64. Toodles
  65. Andy
  66.