From: laviers@IRO.UMontreal.CA (Cocotte Minute)
Newsgroups: comp.lang.c++
Subject: Can't I initialize a dynamically allocated object in its constructor?
Date: 17 Dec 92 20:51:13 GMT
Organization: Universite de Montreal

Hello,

I have the following problem.

 I have created an class: Vector(k), it's
a vector of k dimension (of doubles). After a whole week of debugging, 
I have come to the conclusion that you must not set the values of the elements
 of a class that are dynamically allocated (new) in the constructor.

In the following code, I simply add 3 vectors.
It seems that, in:

   v4=v1+v2+v3,


a temporary vector is constructed to put the result of v1+v2 .
If there is no initialization in the constructor , it works just fine, 
if there is initialization (here, to [-1,-1,-1...]), we seem to loose the value 
of v1+v2... why???


in this example: v1=[1,1,1],v2=[2,2,2],v3=[3,3,3]

without initialization 
  (comment the line "for(int i=0;i<array_size;i++)item[i]=-1.0;")

v4=[6,6,6]; /* great! */

with initialization of vectors:

v4=[2,2,2]; /* not good: v4=[-1,-1,-1]+v3 */



Could this imply that the program gave the temporary (stack?) vector the
value v1+v2 _before_ finishing to allocate (construct) it's space?

Pohl (and others) seem to think that it is good practice to initialize
objects... and have no undefined values.

is this a bug in the compiler? I've only tried it on the C++ v2.1.1
from SGI. If you try this on any other compiler please tell me if you 
get the same results. 

Thank you very much for any help you can give me!

Sebastien Lavier
laviers@iro.umontreal.ca


-------------------------->8 snip, snip 8<-----------------------------

#include <stdio.h> 

//-------------------------------------------------------------------
// Vector: vector of double
//-------------------------------------------------------------------

class Vector{

double 	*item ;
int 	size ;

	
public :
		
// constructors 
Vector() { item = new double ; size = 1 ;}
Vector(int array_size);

// destructors 
~Vector() { delete item ; }

// methods 
int getsize() { return size ; }

void print(char *message);


// operators 
Vector operator+ (Vector& b);
Vector& operator= (Vector& a);
double& operator[] (int i);
};


// constructor

Vector::Vector(int array_size) { 
   item = new double[array_size] ; 
   size = array_size ;
   printf("constructing\n");
   for(int i=0;i<array_size;i++)item[i]=-1.0;
}


Vector Vector::operator+ (Vector& a){
   if (getsize() != a.getsize()){
      printf("operator '+':Vectors are not of the same size.\n");
      exit(0);
   }
   Vector retVect(getsize()) ;
   for (int i = 0 ; i < a.getsize() ; i++){
      retVect.item[i] = item[i] + a.item[i] ;
   }
   return retVect ;
}



Vector& Vector::operator= (Vector& a){
   if (getsize() != a.getsize()){
      printf("operator '=':Vectors are not of the same size.\n");
      exit(0);
   }
   for (int i = 0 ; i < a.getsize() ; i++){
      item[i] = a.item[i] ;
   }
   return *this ;
}


double& Vector::operator[](int i){
   return item[i] ;
}


void  Vector::print(char *message) { 
   printf("%s\n",message);
   for (int i = 0 ; i < size ; i++){
      printf("       [%d]=%lf\n", i, item[i]);
   }
}



main(){

Vector v1(3),v2(3),v3(3),v4(3);



v1[0]=1.0;
v1[1]=1.0;
v1[2]=1.0;

v2[0]=2.0;
v2[1]=2.0;
v2[2]=2.0;

v3[0]=3.0;
v3[1]=3.0;
v3[2]=3.0;

v4=v1+v2+v3;

v4.print("v4");

}

-------------------------->8 snip, snip 8<-----------------------------




--
_____________________________________________________________________________
|Sebastien Lavier [Calimero]    | Centre de recherche, Hopital du Sacre-Coeur  
|laviers@iro.umontreal.ca       | "The opinions expressed here are mine and  
|Universite de Montreal, Canada |   should also be yours!"               
Newsgroups: comp.lang.c++