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.
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".
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.
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:
void func(const test& pTest);
[code]
#include <iostream>
using namespace std;
class test
{
public:
const test& operator=( const test& src ) { return *this; };
void func(const test& pTest);
};
int main()
{
test a, b, c;
c.func(a=b);
return(0);
}
void test :: func(const test& pTest)
{}
[\code]
3) = operator is overloaded automatically for every class unless you want to perform something special other than just copying the values of the member variables.
Last edited by Muthuveerappan; March 13th, 2009 at 03:29 AM.
Reason: Incomplete text
Effective C++ 3rd edition
Item 10: Have assignment operators return a reference to *this
Read it an then talk about it.
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.
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.
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
Thanks,
Last edited by Khaled.Alshaya; March 13th, 2009 at 05:30 PM.
Khaled Alshaya, CS student at KFUPM ..... Just another programmer!
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!).
Bookmarks