Overloading Output for a pointer to an array in a class

Hello all,

I have been trying to write a class that can handle bigInts (up to 40 digits long stored in a dynamic array of shorts) but I am having trouble overloading the addition operator. My logic is 99% of the way there, but the main issue is on line 16 where I am adding 1 to the i-1 of the LHS pointer (for the overflow). While it does the addition correctly after that, it is also editing the LHS itself, which pretty much ruins the whole program. The loop is traversing from right to left as well. I have attached what I have so far below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  myInteger operator+(const myInteger& obj) const
    {
        int holder = 0;
        myInteger temper;

        for (auto i = arrSize; i != 0; i--)
        {
            if(ptr[i] == 0 && obj.ptr[i] == 0)
            {
                continue;
            }
            else
            {
                if(ptr[i]+obj.ptr[i]>9)
                {
                    ptr[i-1] += 1;
                    temper.ptr[i] = (ptr[i]  + obj.ptr[i])-10;
                    holder += 1;
                }
                else
                {
                    temper.ptr[i] = ptr[i] + obj.ptr[i];
                    holder += 1;
                }
            }
        }
        temper.bigness = holder;
        return temper;
    }
Last edited on
Addition is usually implemented as
1
2
3
4
5
6
7
8
N carry = 0;
for i = (least significant position) to (most significant position){
    N x = left[i] + right[i] + carry;
    result[i] = x % modulo;
    carry = x / modulo;
}
if (carry > 0)
    //overflow 
Topic archived. No new replies allowed.