Link to home
Start Free TrialLog in
Avatar of code4susant
code4susant

asked on

Returning a class type or its reference ?

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;
}
ASKER CERTIFIED SOLUTION
Avatar of Cayce
Cayce
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of code4susant
code4susant

ASKER

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
i'd say it *reverse*:  

if possible take use of pointers or references, when passing objects ...