Pointers to objects -- passing and returning pointers
I'm writing a large numbers class just for fun and currently use no pointers. I basically have objects with functions for performing operations and these functions return the answer as another LargeNumber. So
will return another (LargeNumber)c that equals a + b. What I want to do is define it to take and return pointers because this would be (supposedly) faster. I was working with it earlier and got a couple errors about scope and pointers so I figure I was doing it wrong.
(LargeNumber)c = (LargeNumber)a.add((LargeNumber)b);
Here's my multiply function, it's short because it uses add() and other routines, I added a couple comments.
I want to turn this into a function that takes a pointer of a LargeNumber and returns a pointer to another LargeNumber.
LargeNumber LargeNumber::mul(LargeNumber x)
LargeNumber muls = LargeNumber(0,0,0);
muls = simplemul(x.getDigit(x.getLength()));//simplemul multiples the entire number by a single digit
muls.setSign(1);//sign figured out latter, all positive for addition
bool yy = 1;
for(int i = x.getLength() - 1; i > 0; i--)//elementary arithmetic, multiply top by each digit in the bottom...
muls[yy] = simplemul(x.getDigit(i));
muls[yy].mul10(x.getLength() - i); //adds a zero to the right
muls[yy] = muls[yy].add(muls[!yy]);
yy = !yy;
muls[!yy].setDec(getDec() + x.getDec());//finds the decimal
if(getSign() == 0 && x.getSign() == 0)
muls[!yy].setSign(getSign() && x.getSign());
Just for simplicity I don't need you to rewrite the entire function. Just pointify this and/or explain something :)
LargeNumber* LargeNumber::mul(LargeNumber* x)
LargeNumber muls = LargeNumber(0,0,0); //Define muls somehow...
muls = simplemul(x.getDigit(x.getLength()));
//do stuff (I understand x->function()??)
LargeNumber* a = &LargeNumber(...);
LargeNumber* b = &LargeNumber(...)
LargeNumber* c = a->mul(b);
//Alternatively I assume this would work??
LargeNumber a = LargeNumber(...);
LargeNumber b = LargeNumber(...);
LargeNumber* c = a.mul(&b);