We help IT Professionals succeed at work.

Returning a class type or its reference ?

code4susant
code4susant asked
on
300 Views
Last Modified: 2013-11-17
Hi,
 I want to know what is the specific advantages (if any) of using reference of a class as the return type of a function over just the plain class type? I know while passing arguments to a function , reference helps avoid making a copy. But is that possible for a return type.

// return type just the class

class class:: returnme(){
   return *this;
}

// return type a reference

class& class::returnme(){
  return *this;
}
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi,
both the comments helped a lot... so i think ,then the only reason for returning a class type from a function is if you want to return a local object , right ? Beacuse that object can not be accessed by either reference or pointer ('cause it goes out of scope)..

thanks,
Susanta
CERTIFIED EXPERT

Commented:
i'd say it *reverse*:  

if possible take use of pointers or references, when passing objects ...
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.