Return pointer or variable by ref.?

N

Nafai

Hello I would like you guys to give me the best solution for this problem:


class bigInteger {
private:
vector<char> digits;
public:
...

bigInteger& operator+=(const bigInteger& x)
{ ... } // Already implemented.


// WHICH WAY DO I CHOOSE?

// Option 1
bigInteger operator+(const bigInteger& x) const
{
bigInteger y = *this; // ONE COPY
y += x;
return y; // TWO COPIES (?)
}

// Option 2
bigInteger* operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return y;
} // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
using references, pointers, and static variables mixed up everywhere.

// Option 3
bigInteger& operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return *y;
} // It isn't OK. Now I miss garbage collector!

// Option 4
void add(const bigInteger& op, bigInteger& out)
{
out = *this;
out += op;
} // OK, but can't use operator+ and build arithmetic expressions

};


If you have any better solution, tell me!

Thanks.
 
N

Nafai

} // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
using references, pointers, and static variables mixed up everywhere.

I meant: ... but I _would_ like to avoid pointers ...
 
K

Karl Heinz Buchegger

Nafai said:
If you have any better solution, tell me!

Let the optimizer do its work

bigInteger& operator+(const bigInteger& x) const
{
bigInteger y( *this );
y += x;
return y;
}
 
N

Nafai

Karl Heinz Buchegger escribió:
Let the optimizer do its work

bigInteger& operator+(const bigInteger& x) const
{
bigInteger y( *this );
y += x;
return y;
}

But is it OK to return a local variable?
 
K

Karl Heinz Buchegger

Nafai said:
Karl Heinz Buchegger escribió:

But is it OK to return a local variable?

Ooops. Sorry. I didn't want to write that. It happend
during Cut&Paste. You are right of course. The return
type is object, not reference to object.

bigInteger operator+(const bigInteger& x) const
{
bigInteger y( *this );
y += x;
return y;
}
 
N

Nafai

Karl Heinz Buchegger escribió:
Ooops. Sorry. I didn't want to write that. It happend
during Cut&Paste. You are right of course. The return
type is object, not reference to object.

bigInteger operator+(const bigInteger& x) const
{
bigInteger y( *this );
y += x;
return y;
}

So I may suppose that there will be only one copy thanks to the
optimizer. Am I right?
 
K

Karl Heinz Buchegger

Nafai said:
Karl Heinz Buchegger escribió:

So I may suppose that there will be only one copy thanks to the
optimizer. Am I right?

The language standard has nothing to say about that. But in
practice, yes, the optimizer will do a good job and optimize
away the local variable. In fact, if the above function gets inlined,
and there is no reason that it won't, it is one of the simpler
tasks of the optimizer to get rid of it.
 
N

Nafai

Karl Heinz Buchegger escribió:
The language standard has nothing to say about that. But in
practice, yes, the optimizer will do a good job and optimize
away the local variable. In fact, if the above function gets inlined,
and there is no reason that it won't, it is one of the simpler
tasks of the optimizer to get rid of it.

OK, thank-you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top