operator= with non-const reference parameter

D

ded'

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'
 
J

John Harrison

ded' said:
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
 
L

Leor Zolman

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::eek:perator=(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::eek:perator=(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
..
 
B

Buster

ded' said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top