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

function return value

P: n/a
Hello All,
I know that when you pass an argument to a function (if you want let
the
function be able to change the value) then you can choose to pass the
argument by reference or a pointer to that argument.

Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()

Thanks for explaining this.
Robert

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hello,
Passing parameters by reference is not only used for modification purpose.
It's used also to avoid copying for performance issue.You return an argument
by reference for the same reason (be carefull to not returning a refrence to
a temporary )
Ahmed MOHAMED ALI
wo*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hello All,
I know that when you pass an argument to a function (if you want let
the
function be able to change the value) then you can choose to pass the
argument by reference or a pointer to that argument.

Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()

Thanks for explaining this.
Robert

Jul 23 '05 #2

P: n/a
wo*********@yahoo.com wrote:
I know that, when you pass an argument to a function
(if you want let the function be able to [modify] the value)
then you can choose to pass the argument
by reference or a pointer to that argument.

Now my question is,
"Why would you prefer to let a function return a reference or a pointer?"

Consider the following with operator overloading.
I suppose the operator can be seen as a function (right ?)

type operator-()
type& operator-()


1. Don't pass [non-const] references of pointers to functions.
2. If your function *must* modify one of it's arguments,
pass a reference or a pointer to the object then
return a reference or a pointer to that object
*instead of implementing a void function"
so that you can use the function in expressions.

You compiler will *not* be able to resolve an invocation
of operator-() to either of the two candidates above.
You must implement one or the other -- not both.

You will need to explain the [overloaded] meaning
of operator- before we can help you decide
whether to return a value or a reference
but built-in operator- *always* returns a value.
Jul 23 '05 #3

P: n/a
Ahmed MOHAMED ALI wrote:
Passing parameters by reference is not only used for modification purpose.
It's used also to avoid copying for performance issue.
You return an argument by reference for the same reason.


This is a common mistake.
Return by value does *not* require a copy.

Suppose that you have huge objects of type

class Huge { };

The typical optimizing C++ compiler will emit
essentially the same code for a function:

Huge f(void) {
Hugh return_value;
// modify return_value
return return_value;
}

which returns a Huge object by value
as it does for an equivalent function:

Hugh& f(Hugh& return_value) {
// modify return_value
return return_value;
}

which returns a reference to a Huge object.
Jul 23 '05 #4

P: n/a
wo*********@yahoo.com wrote:
Now my question is, why would you prefer to let a function return a
reference or a pointer ???
Consider the following with operator overloading. I suppose the
operator can be seen as a function (right ?)

type operator - ()
type& operator - ()


I think your questions can be best answered by the FAQ. See sections 8
and 13.9.

Kristo

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.