By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Returning by reference

P: 5
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
  2. int &add(int &a)
  3. {    
  4.      a+=1;
  5.      return a;
  6. }
  8. main()
  9. {
  10.     int b;
  11.     int &ref=b;
  12.     add(ref);   // POINT OF DOUBT
  13.     return 0;
  14. }
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
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 4,677
Because you never instantiate b to any number, the behavior is undefined.
Aug 13 '07 #2

P: 5
Because you never instantiate b to any number, the behavior is undefined.

But what if i do instantiate b.
Aug 13 '07 #3

Expert Mod 2.5K+
P: 4,677
But what if i do instantiate b.
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

Expert Mod 5K+
P: 9,197
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

Expert 100+
P: 463
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

Post your reply

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