473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Return pointer or variable by ref.?

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.
Jul 23 '05 #1
8 2065
} // 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 ...
Jul 23 '05 #2
Nafai wrote:


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;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Karl Heinz Buchegger escribió:
Nafai wrote:

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;
}


But is it OK to return a local variable?
Jul 23 '05 #4
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;
}


But is it OK to return a local variable?


But is it OK to return a local variable _by reference_?
Jul 23 '05 #5
Nafai wrote:

Karl Heinz Buchegger escribió:
Nafai wrote:

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;
}


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;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6
Karl Heinz Buchegger escribió:
Nafai wrote:
Karl Heinz Buchegger escribió:
Nafai wrote:
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;
}


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;
}


So I may suppose that there will be only one copy thanks to the
optimizer. Am I right?
Jul 23 '05 #7
Nafai wrote:

Karl Heinz Buchegger escribió:
Nafai wrote:

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?


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8
Karl Heinz Buchegger escribió:
Nafai wrote:
Karl Heinz Buchegger escribió:
Nafai wrote:

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?

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.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bryan Parkoff | last post by:
I set Function Pointer variable to private so unauthorized users cannot access it, but they are allowed to use Get_ and Set_ functions to modify Function Pointer variable. Look at my example. ...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Mohammad Omer | last post by:
I am using vs2k5 for C++ IDE. I need to understand concept of the following Code behavior on VC compiler. Code-1 --- Object getObject() { return Object(); }
4
by: | last post by:
The output is: 1234 After getline: 1234 After renew: 1234 After retnp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝG After getp: ÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝÝG -----What happen after renew/retnp call?------- Why not return a true...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
34
by: Davy | last post by:
Hi all, I am writing a function, which return the pointer of the int. But it seems to be wrong. Any suggestion? int * get_p_t(int t) { return &t; } int main()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.