469,933 Members | 2,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,933 developers. It's quick & easy.

operator= with non-const reference parameter

Hello !

I've read in a magazine "reference parameter in operator= must be const,
because in C++, temporary objects are const" and then my operator would
not work with temporary objets.

But, my compiler doesn't have temporary const objects. Are there any
reasons to have a const reference parameter ?

Thanks in advance for your help

ded'
Jul 22 '05 #1
3 1767

"ded'" <de*@laposte.nospam.net> wrote in message
news:40***************@laposte.nospam.net...
Hello !

I've read in a magazine "reference parameter in operator= must be const,
because in C++, temporary objects are const"
Both half's of that statement are wrong. What was the magazine?

The rule that the magazine was probably trying to express is, temporary
objects cannot be bound to a non-const reference.
and then my operator would
not work with temporary objets.
That's correct.

But, my compiler doesn't have temporary const objects. Are there any
reasons to have a const reference parameter ?


Your thinking is wrong. The question you should ask is 'does my assignment
operator modify the value on the right hand side?' If the answer is no
(which it will be 99.9% of the time) then you should use a const reference.
It's all about better expressing the meaning of your code, not about 'does
it work with this compiler', or 'do I need to do it'.

Same goes for any other function you write, if it takes a reference
parameter and the reference is not used to modify the object then use a
const reference.

john
Jul 22 '05 #2
On Sat, 01 May 2004 18:11:49 +0200, ded' <de*@laposte.nospam.net> wrote:
Hello !

I've read in a magazine "reference parameter in operator= must be const,
because in C++, temporary objects are const" and then my operator would
not work with temporary objets.
Well, sometimes it matters and sometimes it doesn't. Let's say you simply
define operator= as taking a T& as in the following test program:

#include <iostream>
using namespace std;

class T
{
public:
T();
T &operator=(T &) { cout << "In T::operator=(T&)\n";
return *this; }
};

int main()
{
T t;
T t2;
t2 = t;
return 0;
}

It compiles and runs. So obviously either the magazine was wrong or you
misinterpreted what it said. I suspect it is the latter. The cases when
references must be declared const are when you bind them to constant
expressions, such as:
int const &ri = 10;

or:

void f(int &ri) {...}
...
f(10);

But, my compiler doesn't have temporary const objects.
I suspect it does.
Are there any
reasons to have a const reference parameter ?
The usual reason to show a ptr or ref parameter to /any/ function as ptr-to
-const or ref-to-const is for the benefit of "const correctness": to
"document" to the caller of the function that the argument in question will
not be modified. A reference-to-const or pointer-to-const parameter also
makes it possible for the argument passed in that position to actually have
a ptr-to-const or reference-to-const type. If we modify the definition of
t in main() above to be as follows:
const T t;
then the program will no longer compile. But changing the def of operator=
to:
T &operator=(const T &) { cout << "In T::operator=(const T&)\n";
return *this; }
works in all cases, supports const correctness and allows const right-hand
expressions when using operator=. What's not to like?
-leor
..


Thanks in advance for your help

ded'


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
ded' wrote:
I've read in a magazine "reference parameter in operator= must be const,
because in C++, temporary objects are const" and then my operator would
not work with temporary objets.
That's badly phrased. Sometimes it's reasonable to have a non-const
reference parameter to 'operator =', but it's true that in that case you
wouldn't be able to assign from a temporary. This isn't because
"temporary objects are const", it's because temporaries cannot be bound
to non-const references.
But, my compiler doesn't have temporary const objects.
What? Does it accept the following program?

int f () { return 0; }
void g (int & x) { ++ x; }
int main () { g (f ()); }

If so, ditch it and get a better one. Some of the best are free.
Are there any reasons to have a const reference parameter ?


Yes. The parameter should be a non-const reference if the function
modifies its argument and a const reference (or a value) otherwise.

--
Regards,
Buster.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Eckhard Lehmann | last post: by
17 posts views Thread by Chris | last post: by
5 posts views Thread by Dave | last post: by
7 posts views Thread by Alex Vinokur | last post: by
14 posts views Thread by Jess | last post: by
4 posts views Thread by aaragon | last post: by
18 posts views Thread by Ranganath | last post: by
1 post views Thread by subramanian100in | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.