function return value

W

wongjoekmeu

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
 
E

E. Robert Tisdale

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.
 
A

Ahmed MOHAMED ALI

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
(e-mail address removed)> wrote in message
news:[email protected]...
 
E

E. Robert Tisdale

Ahmed said:
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.
 
K

Kristo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top