0

I have two methods:

bigint& bigint::operator=(const bigint& b){
    init();
    for(int i = N-1; i >= N - b.getLength(); i--){
        un[i] = b.un[i];
    }
    setLength(b.getLength());
    return *this;
}

void bigint::init(){
    int L = getLength();
    for(int i = 0; i < L; i++){
        un[N-L+i] = 0;
    }
    setLength(1);
}

First is called operator= in the code, which assigns value of b to *this.

Class bigint represents big integers of max length = N. Instance of bigint contains array of length equal to N.

What's my problem: I can't determine why calling the method init(), which zeros every element of *this, influences also the parametr b. It seems like it zeros also the b.

When I delete the init() calling from the operator= method, b is not modified, but I need to zero the bigint *this before assigning it some value.

I don't understand what's really going on.

Thanks for any help.

Andrew
  • 1
  • 2
  • 4
    Shouldn't be possible, const is const, at least if bigint is const correct. Can you show us more code? Like the call and getLength? Also, you don't call it on itself, like `bigint a = a;`, right? Have you used a debugger so far or printed out variable configurations in between, like before and after the call of init? – Aziuth Mar 30 '17 at 10:40
  • Did you correctly implement the copy constructor? It seems like `un` is a pointer, so the default copy constructor will just copy the pointer, but not the underlying memory. Then it can happen that two different `bigint` objects use the same array for their data. – pschill Mar 30 '17 at 11:28

0 Answers0