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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i'd say it *reverse*:
if possible take use of pointers or references, when passing objects ...
if possible take use of pointers or references, when passing objects ...
ASKER
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