the style of overloaded assignment operator
Hi :)
I am thinking about the assignment operator...
Basically if I want to overload it I code something like this :
Code:
class test{
public:
const test& operator=( const test& src ) { return *this; };
};
now the reason i return a const ref, is that i don't allow the user to do non-sence thing like the following :
Code:
test a, b, c;
(a = b) = c;
i just ommited the self assignment checking :), cuz i wanna talk about the returned ref only in this thread....
which style do you use ? this :
Code:
test& operator=( const test& src )
or
Code:
const test& operator=( const test& src )
I know that with non-const returned ref we could code somthing like this :
Code:
void func( test& obj ){};
.
.
.
func( a = b );
is this a good style ?
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
Quote:
Originally Posted by Khaled.Alshaya
which style do you use ?
The former, since it is canonical while its disadvantage is mainly theoretical.
Re: the style of overloaded assignment operator
Quote:
Originally Posted by
Mybowlcut
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
Quote:
Originally Posted by
yzaykov
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:
Quote:
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:
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.
Re: the style of overloaded assignment operator
You guys are getting on my nerves:
Quote:
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.
Re: the style of overloaded assignment operator
Quote:
Originally Posted by
yzaykov
You guys are getting on my nerves:
Read it an then talk about it.
Whoa! Calm down there; no one is forcing you to read the thread. :rolleyes: I misunderstood what item you were referring to at first, because you weren't clear..
Quote:
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.
Quote:
Originally Posted by
Mybowlcut
Quote:
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!
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 :D
Thanks,
Re: the style of overloaded assignment operator
Quote:
Originally Posted by
Khaled.Alshaya
[COLOR=black][FONT=Verdana]Maybe I am just dipping too much in a syntax difference which doesn't matter as laserlight said :D
Exactly. Besides, any programmer who writes a statement like that deserves a bit of a surprise. :thumbd:
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!).