-
March 12th, 2009, 07:13 PM
#1
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 ?
Khaled Alshaya, CS student at KFUPM ..... Just another programmer!
-
March 12th, 2009, 10:34 PM
#2
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.
-
March 12th, 2009, 11:10 PM
#3
Re: the style of overloaded assignment operator
Originally Posted by Khaled.Alshaya
which style do you use ?
The former, since it is canonical while its disadvantage is mainly theoretical.
-
March 13th, 2009, 02:59 AM
#4
Re: the style of overloaded assignment operator
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".
-
March 13th, 2009, 03:11 AM
#5
Re: the style of overloaded assignment operator
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:
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.
-
March 13th, 2009, 03:17 AM
#6
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.
Last edited by Muthuveerappan; March 13th, 2009 at 03:29 AM.
Reason: Incomplete text
-
March 13th, 2009, 03:26 AM
#7
Re: the style of overloaded assignment operator
You guys are getting on my nerves:
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.
-
March 13th, 2009, 03:57 AM
#8
Re: the style of overloaded assignment operator
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. 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.
-
March 13th, 2009, 05:28 PM
#9
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
Thanks,
Last edited by Khaled.Alshaya; March 13th, 2009 at 05:30 PM.
Khaled Alshaya, CS student at KFUPM ..... Just another programmer!
-
March 14th, 2009, 02:20 AM
#10
Re: the style of overloaded assignment operator
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
Exactly. Besides, any programmer who writes a statement like that deserves a bit of a surprise.
-
March 14th, 2009, 07:53 AM
#11
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!).
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|