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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!newsserver.technet.sg!nuscc!papaya!suresh
  3. From: suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar)
  4. Subject: Re: Can't I initialize a dynamically allocated object in its constructor
  5. Message-ID: <1992Dec22.114251.6663@nuscc.nus.sg>
  6. Originator: suresh@papaya
  7. Sender: usenet@nuscc.nus.sg
  8. Organization: Institute of Systems Science, NUS, Singapore
  9. Date: Tue, 22 Dec 1992 11:42:51 GMT
  10. Lines: 177
  11.  
  12. In article  <1992Dec18.161017.17058@news2.cis.umn.edu>,hansen@myria.cs.umn.edu (David Hansen)  writes:
  13.  
  14. In article <1992Dec17.205113.1180@IRO.UMontreal.CA>, laviers@IRO.UMontreal.CA (C
  15. ocotte Minute) writes:
  16. > |> Hello,
  17. > |>
  18. > |> I have the following problem.
  19. > |>
  20. > |>  I have created an class: Vector(k), it's
  21. > |> a vector of k dimension (of doubles). After a whole week of debugging,
  22. > |> I have come to the conclusion that you must'nt set the values of the element
  23.  
  24.  
  25. :
  26. :
  27. :
  28. > The corrected code appears below.  Enjoy!
  29. :
  30. :
  31. > #include <stdlib.h>    // exit
  32. > //-------------------------------------------------------------------
  33. > // Vector: vector of double
  34. > //-------------------------------------------------------------------
  35. > class Vector{
  36. > double     *item ;
  37. > int     size ;
  38. >     
  39. > public :
  40. >         
  41. > // constructors 
  42. > Vector() { item = new double[1] ; size = 1 ;}    // NOTE: use array form
  43. > Vector(int array_size);
  44. > Vector(const Vector & v);    // NOTE: new copy constructor
  45. > // destructors 
  46. > ~Vector() { delete [] item ; }    // NOTE: use array form
  47. > // methods 
  48. > int getsize() { return size ; }
  49. > void print(char *message);
  50. > // operators 
  51. > Vector operator+ (Vector& b);
  52. > Vector& operator= (Vector& a);
  53. > double& operator[] (int i);
  54. > };
  55. > // constructors
  56. > Vector::Vector(int array_size) { 
  57. >    item = new double[array_size] ; 
  58. >    size = array_size ;
  59. >    printf("constructing\n");
  60. >    for(int i=0;i<array_size;i++)item[i]=-1.0;
  61. > }
  62. > // Definition of new copy constructor
  63. > Vector::Vector(const Vector& v) {
  64. >    int i;
  65. >    size = v.size;
  66. >    item = new double[size];
  67. >    for (i=0; i<size; i++)
  68. >       item[i] = v.item[i];
  69. > }
  70. > Vector& Vector::operator= (Vector& a){
  71. >    if (getsize() != a.getsize()){
  72. >       printf("operator '=':Vectors are not of the same size.\n");
  73. >       exit(0);
  74. >    }
  75. >    for (int i = 0 ; i < a.getsize() ; i++){
  76. >       item[i] = a.item[i] ;
  77. >    }
  78. >    return *this ;
  79. > }
  80. > double& Vector::operator[](int i){
  81. >    return item[i] ;
  82. > }
  83. > void  Vector::print(char *message) { 
  84. >    printf("%s\n",message);
  85. >    for (int i = 0 ; i < size ; i++){
  86. >       printf("       [%d]=%lf\n", i, item[i]);
  87. >    }
  88. > }
  89. > main(){
  90. > Vector v1(3),v2(3),v3(3),v4(3);
  91. > v1[0]=1.0;
  92. > v1[1]=1.0;
  93. > v1[2]=1.0;
  94. > v2[0]=2.0;
  95. > v2[1]=2.0;
  96. > v2[2]=2.0;
  97. > v3[0]=3.0;
  98. > v3[1]=3.0;
  99. > v3[2]=3.0;
  100. > v4=v1+v2+v3;
  101. > v4.print("v4");
  102. > }
  103. > Vector Vector::operator+ (Vector& a){
  104. >    if (getsize() != a.getsize()){
  105. >       printf("operator '+':Vectors are not of the same size.\n");
  106. >       exit(0);
  107. >    }
  108. >    Vector retVect(getsize()) ;
  109. >    for (int i = 0 ; i < a.getsize() ; i++){
  110. >       retVect.item[i] = item[i] + a.item[i] ;
  111. >    }
  112. >    return retVect ;
  113. > }
  114. My apologies for including  the entire code above ....
  115. Also to make things easier I have moved the function  
  116. Vector Vector::operator+() to the bottom.
  117.  
  118. My point:
  119.  
  120. retVect used above is a automatic variable that will go out of scope
  121. as soon as the function Vector::operator+() is exited and it's 
  122. destructor will be called which will deallocate the memory
  123. that was allocated by the constructor. Although the returned
  124. Vector object is a copy and will have a valid item pointer, it will 
  125. be pointing to deallocated data.
  126.  
  127.  
  128. Nitpicking:
  129.  
  130. The operator= function should check for self-assignment
  131. like so   " if ( this != &a) ". Saves a lot of work
  132.  
  133.  
  134.  
  135.       __                  
  136.      (_   / /  o_   o  o |_
  137.      __)/(_( __) (_(_ /_)| )_
  138.  
  139.  
  140.  
  141. ***************************************************************************
  142. * Suresh Thennarangam               *  EMail: suresh@iss.nus.sg(Internet) *
  143. * Research Scholar                  *         ISSST@NUSVM.BITNET          *
  144. * Institute Of Systems Science      *  Tel:  (065) 772 2588.              *
  145. * National University Of Singapore  *  Facs.: (065) 778 2571          *
  146. * Heng Mui Keng Terrace             *  Telex: ISSNUS RS 39988             *
  147. * Singapore 0511.                   *                                     *
  148. ***************************************************************************
  149.