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

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!olivea!charnel!sifon!CC.UMontreal.CA!IRO.UMontreal.CA!brisay.IRO.UMontreal.CA!laviers
  2. From: laviers@IRO.UMontreal.CA (Cocotte Minute)
  3. Newsgroups: comp.lang.c++
  4. Subject: Can't I initialize a dynamically allocated object in its constructor?
  5. Message-ID: <1992Dec17.205113.1180@IRO.UMontreal.CA>
  6. Date: 17 Dec 92 20:51:13 GMT
  7. Sender: news@IRO.UMontreal.CA
  8. Organization: Universite de Montreal
  9. Lines: 170
  10.  
  11. Hello,
  12.  
  13. I have the following problem.
  14.  
  15.  I have created an class: Vector(k), it's
  16. a vector of k dimension (of doubles). After a whole week of debugging, 
  17. I have come to the conclusion that you must not set the values of the elements
  18.  of a class that are dynamically allocated (new) in the constructor.
  19.  
  20. In the following code, I simply add 3 vectors.
  21. It seems that, in:
  22.  
  23.    v4=v1+v2+v3,
  24.  
  25.  
  26. a temporary vector is constructed to put the result of v1+v2 .
  27. If there is no initialization in the constructor , it works just fine, 
  28. if there is initialization (here, to [-1,-1,-1...]), we seem to loose the value 
  29. of v1+v2... why???
  30.  
  31.  
  32. in this example: v1=[1,1,1],v2=[2,2,2],v3=[3,3,3]
  33.  
  34. without initialization 
  35.   (comment the line "for(int i=0;i<array_size;i++)item[i]=-1.0;")
  36.  
  37. v4=[6,6,6]; /* great! */
  38.  
  39. with initialization of vectors:
  40.  
  41. v4=[2,2,2]; /* not good: v4=[-1,-1,-1]+v3 */
  42.  
  43.  
  44.  
  45. Could this imply that the program gave the temporary (stack?) vector the
  46. value v1+v2 _before_ finishing to allocate (construct) it's space?
  47.  
  48. Pohl (and others) seem to think that it is good practice to initialize
  49. objects... and have no undefined values.
  50.  
  51. is this a bug in the compiler? I've only tried it on the C++ v2.1.1
  52. from SGI. If you try this on any other compiler please tell me if you 
  53. get the same results. 
  54.  
  55. Thank you very much for any help you can give me!
  56.  
  57. Sebastien Lavier
  58. laviers@iro.umontreal.ca
  59.  
  60.  
  61. -------------------------->8 snip, snip 8<-----------------------------
  62.  
  63. #include <stdio.h> 
  64.  
  65. //-------------------------------------------------------------------
  66. // Vector: vector of double
  67. //-------------------------------------------------------------------
  68.  
  69. class Vector{
  70.  
  71. double     *item ;
  72. int     size ;
  73.  
  74.     
  75. public :
  76.         
  77. // constructors 
  78. Vector() { item = new double ; size = 1 ;}
  79. Vector(int array_size);
  80.  
  81. // destructors 
  82. ~Vector() { delete item ; }
  83.  
  84. // methods 
  85. int getsize() { return size ; }
  86.  
  87. void print(char *message);
  88.  
  89.  
  90. // operators 
  91. Vector operator+ (Vector& b);
  92. Vector& operator= (Vector& a);
  93. double& operator[] (int i);
  94. };
  95.  
  96.  
  97. // constructor
  98.  
  99. Vector::Vector(int array_size) { 
  100.    item = new double[array_size] ; 
  101.    size = array_size ;
  102.    printf("constructing\n");
  103.    for(int i=0;i<array_size;i++)item[i]=-1.0;
  104. }
  105.  
  106.  
  107. Vector Vector::operator+ (Vector& a){
  108.    if (getsize() != a.getsize()){
  109.       printf("operator '+':Vectors are not of the same size.\n");
  110.       exit(0);
  111.    }
  112.    Vector retVect(getsize()) ;
  113.    for (int i = 0 ; i < a.getsize() ; i++){
  114.       retVect.item[i] = item[i] + a.item[i] ;
  115.    }
  116.    return retVect ;
  117. }
  118.  
  119.  
  120.  
  121. Vector& Vector::operator= (Vector& a){
  122.    if (getsize() != a.getsize()){
  123.       printf("operator '=':Vectors are not of the same size.\n");
  124.       exit(0);
  125.    }
  126.    for (int i = 0 ; i < a.getsize() ; i++){
  127.       item[i] = a.item[i] ;
  128.    }
  129.    return *this ;
  130. }
  131.  
  132.  
  133. double& Vector::operator[](int i){
  134.    return item[i] ;
  135. }
  136.  
  137.  
  138. void  Vector::print(char *message) { 
  139.    printf("%s\n",message);
  140.    for (int i = 0 ; i < size ; i++){
  141.       printf("       [%d]=%lf\n", i, item[i]);
  142.    }
  143. }
  144.  
  145.  
  146.  
  147. main(){
  148.  
  149. Vector v1(3),v2(3),v3(3),v4(3);
  150.  
  151.  
  152.  
  153. v1[0]=1.0;
  154. v1[1]=1.0;
  155. v1[2]=1.0;
  156.  
  157. v2[0]=2.0;
  158. v2[1]=2.0;
  159. v2[2]=2.0;
  160.  
  161. v3[0]=3.0;
  162. v3[1]=3.0;
  163. v3[2]=3.0;
  164.  
  165. v4=v1+v2+v3;
  166.  
  167. v4.print("v4");
  168.  
  169. }
  170.  
  171. -------------------------->8 snip, snip 8<-----------------------------
  172.  
  173.  
  174.  
  175.  
  176. --
  177. _____________________________________________________________________________
  178. |Sebastien Lavier [Calimero]    | Centre de recherche, Hopital du Sacre-Coeur  
  179. |laviers@iro.umontreal.ca       | "The opinions expressed here are mine and  
  180. |Universite de Montreal, Canada |   should also be yours!"               
  181.