Public Group

# Benefits of returning an alias

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Fraction& operator++()
{
num += den;
return *this;
}

and then goes on to say
Quote:
 Notice that the return type of the method had also been modified. This is not strictly necessary but does avoid the cost of an additional copy. With the reference, the assignment is direct. If the method was declared to return Fraction rather than Fraction& an additional copy is made of the returned object. This copy is the assigned to the left hand side of the expression.
The problem is that I can't imagine a picture in my head of how this works. eg. a = function(b); return by value * function returns a value * a copy of the value is made * this copy is assigned to variable a return by reference * function returns a value which is copied directly to variable a since the return value is stored in the same memory address as where variable a resides Now if above reasoning is correct, is there then any reason to ever return by value? Why not return by reference all the time? EDIT: so you return by value when you're returning an object that goes out of scope after the return? and for everything else you return by reference?

##### Share on other sites
Well. I suppose that returning by value restricts the calling codes ability to "change" the underlying data structure.

It can change it's copy, but not the value within the class/object. Saying that you could return by constant reference.

##### Share on other sites
Quote:
 Original post by ShaiNow if above reasoning is correct, is there then any reason to ever return by value? Why not return by reference all the time?

I think that returning by value is a good thing. You can make objects so that you can never change the object you currently have. As an example, take a look at a madeup example...
Money five(5.0);Money eight = five.add(3.0);

If you return a reference, that mean the eight object is really just a reference to the five object, but both of the objects is now 8.0. This is not what I would expect. If you return by value, then the new value returned from the add() function would be eight, but the original value cannot be changed.

##### Share on other sites
There is a general rule which was coined by Scott Myers in one of his many C++ books which will get you out of the woods most of the time:

When is doubt, do as the ints do.

Basically, if it was an int the following would be true...

type_name i( 0 );

++ i += 1; is legal, i now stores 2 (dubious style, I admit)
i ++ += 1; is illeagal.

For ints, ++i generates an l-value, i.e. it is writable (in this case, i itself will be modified), whereas i++ generates an r-value which means it can't be assigned to.

If your prefix ++ operator returns by value, it will be a legal l-value but it wont change your original object thus it does not behave like a standard type. And I think as a general rule we can say that numeric classes whose operators obey different symantic rules to the standard types are pretty damned sinister.

1. 1
2. 2
3. 3
frob
14
4. 4
5. 5
Rutin
12

• 12
• 9
• 57
• 14
• 15
• ### Forum Statistics

• Total Topics
632112
• Total Posts
3004176

×