# Thread: Pointers to objects -- passing and returning pointers

1. ## 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
Code:
`(LargeNumber)c = (LargeNumber)a.add((LargeNumber)b);`
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.

Here's my multiply function, it's short because it uses add() and other routines, I added a couple comments.
Code:
```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].setSign(1);
muls[yy].mul10(x.getLength() - i); //adds a zero to the right
yy = !yy;
}
muls[!yy].setDec(getDec() + x.getDec());//finds the decimal
if(getSign() == 0 && x.getSign() == 0)
muls[!yy].setSign(1);
else
muls[!yy].setSign(getSign() && x.getSign());
return muls[!yy];
}```
I want to turn this into a function that takes a pointer of a LargeNumber and returns a pointer to another LargeNumber.

Just for simplicity I don't need you to rewrite the entire function. Just pointify this and/or explain something Code:
```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()??)
return muls[yy];
}```
Code:
```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);``` 2. Code:
```  if(getSign() == 0 && x.getSign() == 0)
muls[!yy].setSign(1);
else
muls[!yy].setSign(getSign() && x.getSign());```
will be more correctly as:
Code:
`    muls[!yy].setSign(getSign() != x.getSign());`
That sets sign to 1 if one of the signs is non-zero but the other one is zero.

Also, if you are going to use pointers, you are probably better off with a three-way-operator function (where one operand is "this", of course), such as this:
Code:
```void LargeNumber::mul(LargeNumber *x, LargeNumber *res)
{
...
}```
Just beware that "res" could be the same variable as y or this.

--
Mats 3. What's the "proper" way to do this? I've seen code in other projects that make heavy use of pointers to objects. I figure the way I'm currently doing it is inefficient because two object are being copied every time I call a function. You're recommending something like this?
Code:
```LargeNumber a = LargeNumber(...);
LargeNumber b = LargeNumber(...);
LargeNumber c = LargeNumber(...);
a.mul(&b, &c); //give c the value of a times b```
This isn't the way I always see it being done... The functions of course could somehow be static (like you can do in Java) or not attached to the objects so you could do
Code:
`LargeNumber.mul(&a, &b, &c); //multiply a and b, give c the value.`
Btw how does new work? http://en.wikipedia.org/wiki/New_(C%2B%2B)
I'm trying to get a little more in depth with C++. 4. I figure the way I'm currently doing it is inefficient because two object are being copied every time I call a function.
Generally, you can avoid copying by passing by (const) reference. You could then do something like:
Code:
```LargeNumber LargeNumber::mul(const LargeNumber& rhs) const;
// ...
LargeNumber a(...);
LargeNumber b(...);
LargeNumber c = a.mul(b);``` 5. I would make mul a free function and use it like this (you could implement it directly or use an idea that follows a common operator+ idiom):
Code:
```void LargeNumber::multiply_by(const LargeNumber& rhs)
{
// implementation ...
}

LargeNumber multiply(const LargeNumber & lhs, const LargeNumber& rhs)
{
LargeNumber temp(lhs);
temp.multiply_by(rhs);
return temp;
}

...

LargeNumber a(...);
LargeNumber b(...);
LargeNumber c = multiply(a, b);```
Note that the return value of your mul function should not be a pointer or reference, because by multiplying two objects you are creating a third object that is the result. You use pointers and references only to objects that already exist. Popular pages Recent additions 