473,396 Members | 1,995 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,396 software developers and data experts.

Returning by reference

Hello friends

What is the use of returning by reference , other than avoiding call to the copy constructor for a large object.

AND Please explain this code:-
Expand|Select|Wrap|Line Numbers
  1. // FUNCTION DEFINITION
  2. int &add(int &a)
  3. {    
  4.      a+=1;
  5.      return a;
  6. }
  7.  
  8. main()
  9. {
  10.     int b;
  11.     int &ref=b;
  12.     add(ref);   // POINT OF DOUBT
  13.     return 0;
  14. }
  15.  
In the above code add function returns reference to integer , now when i call it in main i give no lvalue , so what will happen to reference returned by function.
Aug 13 '07 #1
5 1337
sicarie
4,677 Expert Mod 4TB
Because you never instantiate b to any number, the behavior is undefined.
Aug 13 '07 #2
Because you never instantiate b to any number, the behavior is undefined.

But what if i do instantiate b.
b=5;
Aug 13 '07 #3
sicarie
4,677 Expert Mod 4TB
But what if i do instantiate b.
b=5;
Did you try putting it into a compiler? I would imagine that would give you the answer rather quickly (more quickly than posting on a forum, perhaps?). And if you really want to know what is going on, you can insert cout or printf statements and see what the value is.
Aug 13 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
You have an object that has a container as a member.

You can return a pointer to an element of that container.

The caller dereferences the pointer and changes the element in the container.

So, you can return a reference to the element and save the trouble of dereferencing the pointer.

Same rules apply: Never return a pointer to a local variable.
Never return a reference to a local variable.
Aug 14 '07 #5
RRick
463 Expert 256MB
Returning something by reference "usually" means that the orginating object/method keeps control of the life of that "something". This means the orginating object guarantees that the reference is valid after it is returned to the calling routine. That's why a reference to a local object is a no-no and is typically a disaster (i.e. crash & burn).

As stated above, objects usually return references to other things within or controlled by the object. As long as the object stays alive, the reference needs be valid. The major exception to this rule is when you tell the object to delete the thing in the reference. Now all bets are off.

Returning pointers in C++ "usually" means the object is passing ownership of the thing to you. Now you control the life span of passed thing.

"Usually" is my way of saying that these rules are made by the developer and are not enforced by the compiler. This means they can be (and are) broken at will.
Aug 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
6
by: EvilOldGit | last post by:
const Thing &operator++(int) { Thing temp = *this; operator++(); return temp; } Is this code robust ? I get a compiler warning about returning a reference to a a local, which I guess is...
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.