If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: the style of overloaded assignment operator

I've never really thought about it. Effective C++ is pretty much my programming bible and it doesn't give this idea a mention. You'll still get people doing assignment when they meant to do equality when you return a const& from operator=, so I guess I don't think it's worth it apart from if you are against chaining assignments.

Re: the style of overloaded assignment operator

I've never really thought about it. Effective C++ is pretty much my programming bible and it doesn't give this idea a mention.

As far as I remember it does. Or it was More Effective C++ but Scott Meyers is definitely the one who says "do like ints do".

@Khaled.Alshaya, there are same famous C++ books like C++_How_to_Program_5_Ed that claim it should return a const reference, but I'd prefer to listen to Scott Meyers, who says that when we wonder about designing a type it's a nice idea to see how it is done in the standard library or in the built-in types. Well, it's legal to write "int i, j; (i = j) = 5;" so what your operator= should return is rather a non-const reference. Still you'll bump into people arguing about it, but as laserlight says "its disadvantage is mainly theoretical".

Re: the style of overloaded assignment operator

As far as I remember it does. Or it was More Effective C++ but Scott Meyers is definitely the one who says "do like ints do".

In relation to the copy assignment operator he doesn't, but he does talk about what you mention in another item:

Originally Posted by Item 20: Prefer pass-by-reference-to-const to pass-by-value

If you peek under the hood of a C++ compiler, you'll find that references are typically implemented as pointers, so passing something by reference usually means really passing a pointer. As a result, if you have an object of a built-in type (e.g., an int), it's often more efficient to pass it by value than by reference.

I wonder just how small the overhead is though... it couldn't be much.

Re: the style of overloaded assignment operator

I think all of you have summed up pretty well.

I have just pasted below my thoughts, correct me if I am wrong:

1) returning by const reference is best if you intend not to change the value of the object returned.
2) if you want to use the const reference returned object as an argument to func, it is possible (see below):

Just define the function func to accept const reference as its argument:

Re: the style of overloaded assignment operator

And something else... Mybowlcut, are you sure the compiler is always implementing references as pointers? I believe it's able not to allocate any memory and just add another name in the nametable to the same piece of memory when possible.

Re: the style of overloaded assignment operator

Whoa! Calm down there; no one is forcing you to read the thread. I misunderstood what item you were referring to at first, because you weren't clear..

Originally Posted by yzaykov

And something else... Mybowlcut, are you sure the compiler is always implementing references as pointers? I believe it's able not to allocate any memory and just add another name in the nametable to the same piece of memory when possible.

Originally Posted by Mybowlcut

Originally Posted by Item 20: Prefer pass-by-reference-to-const to pass-by-value

If you peek under the hood of a C++ compiler, you'll find that references are typically implemented as pointers, so passing something by reference usually means really passing a pointer. As a result, if you have an object of a built-in type (e.g., an int), it's often more efficient to pass it by value than by reference.

Re: the style of overloaded assignment operator

You see guys; Scott Meyers is saying do it as the built-in types did it. I don't understand why?!

Cuz if I coded something like this:

Code:

test& operator=( const test& src )

Then this statement is true!

Code:

(a = b) = c;

after execution a == c only, which is not intended by a programmer who wants to do chaining... whereas if I return a const ref the compiler would issue an error saying that you can’t assign c to const a& ....

I checked Absolute C++ and Effective C++, and they don't talk about the subject in detail,

Maybe I am just dipping too much in a syntax difference which doesn't matter as laserlight said

Re: the style of overloaded assignment operator

You see, it's very unlikely to put braces in "(a = b) = c;" and not want this effect. C++ is a language where many things are allowed and if the programmer wants to do it this way, why put a ban on that? These braces mean that he wants it! Putting the const only limits the developers (in this very case!).