I'm working on an arbitrary precision decimal class. I have everything working so far, but when I try to overload the = operator weird stuff happens. I can assign values to another variable of the same class, but only once. After that the values start to become messed up. There's some examples of what I mean below. The values of dub1 + dub2 does not add up to an integer of 4, but 3 dub1's do from the previous = assignment above the ret2 = dub1 + dub2. If I do only the first assignment (they're in blue) then it works correctly. The addition and subtraction both work correctly given its not assigned to something twice. All the code is there in case anyone needs a reference. The important parts are in blue.

Code:

//this typedef is to be used inside the class _double only

typedef unsigned int *__double;

//templated class for _double

//MAXSIZE is the maximum precision of the value of _double

//the first element in the array is considered the integer

//the numbers from position 1 to MAXSIZE are considered decimals

template<int MAXSIZE> class _double

{

private:

__double bigdouble;// = new int[MAXSIZE];

public:

_double(); //constructor

_double(int i, int* d, int size); //secondary constructor

~_double(); //destructor

void copy(_double a); //copies b to a

//begin addition operator overloading

friend _double operator + (const _double a, const _double b)

{

_double<MAXSIZE> ret; //return value

int carry = 0;

for(int i = MAXSIZE - 1; i >= 0; i--){

ret.bigdouble[i] = ( a.bigdouble[i] + b.bigdouble[i] + carry ) % 10; //find digit for current position

carry = ( a.bigdouble[i] + b.bigdouble[i] + carry ) / 10; //find carry over digit

}

return ret;

} //end operator+

friend _double operator * (const _double a, const _double b)

{

_double<MAXSIZE> ret;

return ret;

}

friend _double operator - (const _double a, const _double b)

{

_double<MAXSIZE> ret;

for(int i = MAXSIZE - 1; i >= 0; i--){

if(a.bigdouble[i] >= b.bigdouble[i])

ret.bigdouble[i] = a.bigdouble[i] - b.bigdouble[i];

else if(a.bigdouble[i] < b.bigdouble[i]){

int j = i-1;

while(a.bigdouble[j] == 0){ j--; }

a.bigdouble[j] = a.bigdouble[j] - 1;

for(j=j+1; j <= i; j++){

a.bigdouble[j] += 10;

if(j != i) a.bigdouble[j] -= 1;

}

ret.bigdouble[i] = a.bigdouble[i] - b.bigdouble[i];

}

}

return ret;

}

_double operator = (const _double a) const

{

_double<MAXSIZE> ret;

for(int i = 0; i < MAXSIZE; i++)

ret.bigdouble[i] = a.bigdouble[i];

return ret;

}

void print(); //print the value of _double

};

template<int MAXSIZE> _double<MAXSIZE>::_double()

{

bigdouble = new unsigned int[MAXSIZE]; //allocate required space

//set the entire array to zero

for(int i = 0; i < MAXSIZE; i++){

//if(i >= MAXSIZE) break; //safe guard just in case

bigdouble[i] = 0; //set to zero

}

} //end constructor

template<int MAXSIZE> _double<MAXSIZE>::_double(int i, int d[], int size)

{

bigdouble = new unsigned int[MAXSIZE]; //allocate required space

bigdouble[0] = i;

//set the entire array to zero

for(int i = 1; i < MAXSIZE; i++){

if(i-1 < size)

bigdouble[i] = d[i-1]; //set to zero

else

bigdouble[i] = 0;

}

} //end constructor

template<int MAXSIZE> _double<MAXSIZE>::~_double(){ delete [] bigdouble; } //delete allocated space

//prints all the digits that are stored in the array

template<int MAXSIZE> void _double<MAXSIZE>::print()

{

cout<<bigdouble[0]<<"."<<endl;

for(int i = 1; i < MAXSIZE; i++)

cout<<bigdouble[i];

}

//copies the contents of a

template<int MAXSIZE> void _double<MAXSIZE>::copy(_double a)

{

for(int i = MAXSIZE - 1; i >= 0; i--)

bigdouble[i] = a.bigdouble[i];

}

//the main program

int main()

{

int decimal[] = {3,4,7};

_double<10> dub1(1,decimal,3);

int decimal2[] = {2,6,9};

_double<10> dub2(0,decimal2,3);

dub1.print(); cout<<endl<<endl;

dub2.print(); cout<<endl<<endl;

_double<10> ret = dub1;// + dub1;

_double<10> ret2 = (dub1 + ret) + ret;

ret2 = dub1 + dub2;

ret2.print();

return 0;

}

//The output from above int main() code

1.

347000000

0.

269000000

4.

041000000

Process returned 0 (0x0) execution time : 0.016 s

Press any key to continue.