Hello again,
now i have some trouble with the dynamic allocation of memory...i.e.:
I want to create a class, that contains a variable sized field of numbers and a field containing the length of the field....say
1 2 3 4 5 6 7 8 9 10 11
|
class dynmem {
double *vkt;
int len;
public:
// Constructor
dynmem();
dynmem(int, double*);
//Destructor
~dynmem();
/*etc. pp........................*/
|
I create an operator for my class that adds the elements of two classmembers (as long as index of the smaller not exceeded) and makes a new "dynmem"-object with the size of the bigger one (field are filled).
The operator looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
dynmem* dynmem:: operator +(dynmem as){
dynmem out;
//nun hier die Felder hernehmen:
int len1=getLen();
int len2 = as.getLen();
int lan=(int)max((double)len1, (double)len2);
double *vals=new double[lan];
for (int l=0; l<len1; l++){
vals[l]=getVals()[l] + as.getVals()[l];
}
for(int l=len1; l<lan; l++){
if(len1<len2){vals[l] = as.getVals()[l];}
else{vals[l] = getVals()[l];}
}
out.setVals(lan, vals);
return &out;
}
|
So, it returns a pointer to the newly created "dynmem" object.....
however, it doesn't work properly. It returns such pointer, but afterwards, I can't dereferenciate it.....any idea ??
It doesn't work if it tries to return a "dynmem"-object, so i decided to let it return a pointer to such an object....
p.s.: My efforts to dereferenciate...
|
f = (*out).getVals()[7];....
|
Is my problem that my object has yet been deleted ? However, if it's so, why doesn't it work, if i try to return an object and not a pointer ?
Could the Constructor or the destructor be the problem ?
Maybe i'll find out on my own....but it would take long i guess...
Greetings and Thanks so far for reading this. I am thankful for any hint.