I am trying to implement a swap function, that overrides the copy constructor and ultimately will allow me to use the equal '=' operator. The issue is when testing my swap function I get junk values for everything in other(isNeg is a bool, digits is an unsiged int* array, and numDigits is the size of
digits.
A ReallyLongInt is an Unsigned Int* array of size numDigits, I have thoroughly tested my constructors and they work for both Strings and long longs.
Here is the declaration (ReallyLongInt.cc) :
void ReallyLongInt::swap(ReallyLongInt other)
{
//Sets temporary values to equal others values
unsigned int* temp = new unsigned int[this->numDigits];
std::swap(this->digits, other.digits);
std::swap(this->numDigits, other.numDigits);
std::swap(this->isNeg, other.isNeg);
}
and my main which I use to test (main.cc)
#include "ReallyLongInt.h"
#include <iostream>
using namespace std;
int main(int argc, char** argv){
string a = "56789";
long long b = 123456;
ReallyLongInt x = ReallyLongInt(a);
ReallyLongInt y = ReallyLongInt(b);
cout<<"a: "<<x<<endl;
cout<<"b: "<<y<<endl;
y.swap(x);
cout <<"b after swapping with a: "<< y <<endl;
}
I believe the problem lies in the value I pass to my swap call
y.swap(x)
but when I run the code the size of this->numDigits is junk, and I get a segFault because of this number interfering with my print function.