Why is const needed in member function?

I

Immortal Nephi

I read Deitel How to program C++ Edition 6. It talks about operator=
member function. I wonder why const is necessary? For example

**const** Test &Test::eek:perator=( const Test &rc_Test )

Notice const in between pair of asterisk. What is this for?

The const with rc_Test in the parameter is best practice.
 
A

Alf P. Steinbach

* Immortal Nephi:
I read Deitel How to program C++ Edition 6. It talks about operator=
member function. I wonder why const is necessary? For example

**const** Test &Test::eek:perator=( const Test &rc_Test )

Notice const in between pair of asterisk. What is this for?

It's not part of the C++ source code.

Probably in the book it's set in some other font or color.

Exactly what it refers to is unclear from the limited context you provide.


The const with rc_Test in the parameter is best practice.

Well, operator= shouldn't have a reference-to-non-const formal argument, but
that's all.

I don't think "best practice" applies.

But it's very common, yes.


Cheers & hth.,

- Alf
 
S

SG

I read Deitel How to program C++ Edition 6.  It talks about operator=
member function.  I wonder why const is necessary?  For example

**const** Test &Test::eek:perator=( const Test &rc_Test )

Notice const in between pair of asterisk.

Without further context about how Test should behave I have to say:
No, this const is unusual, not necessary, and it doesn't help unless
you explicitly want to prevent multiple assignment.

Cheers!
SG
 
J

James Kanze

On 16 Aug., 22:47, Immortal Nephi <[email protected]>
wrote:
Without further context about how Test should behave I have to
say: No, this const is unusual, not necessary, and it doesn't
help unless you explicitly want to prevent multiple
assignment.

It doesn't prevent multiple assignments. It does prevent some
stupid errors, but not (IMHO) enough to justify violating the
expectations of operator=. The built-in operator= results in a
non-const lvalue, so so should user defined operators.

Whether the build-in operator= should result in a non-const
lvalue or not is, of course, highly debatable. In C, it
doesn't, and if you wanted to duplicate the semantics of
assignment in C, you're probably return a const reference.
 
J

Juha Nieminen

James said:
It doesn't prevent multiple assignments. It does prevent some
stupid errors, but not (IMHO) enough to justify violating the
expectations of operator=. The built-in operator= results in a
non-const lvalue, so so should user defined operators.

To actualize that paragraph: operator= returning a const reference
allows doing this:

value1 = value2 = value3;

but forbids doing this:

(value1 = value2) = value3;

If it returns a non-const reference, then the latter is ok as well.
Note, however, that the meaning of those two lines is completely
different! The end result is not the same.
 
A

Alf P. Steinbach

* Juha Nieminen:
To actualize that paragraph: operator= returning a const reference
allows doing this:

value1 = value2 = value3;

but forbids doing this:

(value1 = value2) = value3;

If it returns a non-const reference, then the latter is ok as well.
Note, however, that the meaning of those two lines is completely
different! The end result is not the same.

Good point.

I hadn't thought of that, and judging by the almost exlusively T& result
assignment operators around, just about nobody else has either (although
presumably the author of the book the OP mentioned, Mr. Deitel, had).

I think I'll adopt that convention, for expressions with internal side effects
are generally ungood.


Cheers,

- Alf


PS: I now understand that the asterisks in the code that the OP quoted, were
probably put there by the OP, for emphasis. But added asterisks are a bit
misleading in C++ source code! :)
 
J

James Kanze

To actualize that paragraph: operator= returning a const
reference allows doing this:
value1 = value2 = value3;
but forbids doing this:
(value1 = value2) = value3;

Which isn't legal for the built-in operator= either (since it
results in modifying the same object twice without an
intervening sequence point). Arguably, forbidding this is an
argument in favor of the const reference.
If it returns a non-const reference, then the latter is ok as
well. Note, however, that the meaning of those two lines is
completely different! The end result is not the same.

Yes, but I've never seen nor heard of anyone who would write the
second.

In C++, a built-in assignment operator results in an lvalue.
About the only legal use for this is to bind the results to a
reference, e.g.:

MyType&
operator+=( MyType& lhs, MyType rhs )
{
return lhs = lhs + rhs ;
}

I find this poor style, but others disagree. If operator=
returns a non-const reference, this code is legal; if it returns
a const reference, it's not.
 
J

James Kanze

* Juha Nieminen:
Good point.
I hadn't thought of that, and judging by the almost exlusively
T& result assignment operators around, just about nobody else
has either (although presumably the author of the book the OP
mentioned, Mr. Deitel, had).

That's because you're too young. When I was just starting C++,
it was a much debated point; all of my original operator=
returned const references. After some discussion with Scott
Meyer, I changed my policy---on the sole grounds that a
non-const reference is closer to what the built-in operator
does, and so closer to what users expect. The only more or less
valid use for it is in something like:

MyType&
operator+=( MyType& lhs, MyType rhs )
{
return lhs = lhs + rhs ;
}

In such cases, I far prefer using two statements, but I'm not
the only person in the world, and since this works with the
built in operator= (or the compiler generated default one),
people reasonable expect it to work with a user defined one.
I think I'll adopt that convention, for expressions with
internal side effects are generally ungood.

As I said, the point was strongly argued in the early 1990's.
Today, there seems to be a consensus for the non-const reference
(perhaps due to the influence of people like Scott Meyers). And
of course, you can't put an object whose operator= returns a
const reference in a standard container---the Assignable
requirements are that the expression t = u returns T&.
(Formally, at least. It works with both g++ and VC++, including
with concept checking activated in g++. Presumable, however, if
concepts ever get into the standard, all compilers will be
required to reject it.)

In the end, the point which convinced me (somewhat against my
better judgement) was the fact that when moving from a compiler
generated assignment operator to a user defined one, you don't
want the function signature to change, especially in a way that
might break user code (even if you don't like the style of said
user code).
 
N

Noah Roberts

Immortal said:
I read Deitel How to program C++ Edition 6. It talks about operator=
member function. I wonder why const is necessary? For example

**const** Test &Test::eek:perator=( const Test &rc_Test )

Notice const in between pair of asterisk. What is this for?

The const with rc_Test in the parameter is best practice.

They don't want you to be able to do things like so:

Test t1;
Test t2;

// change state of t2...

(t1 = t2).do_something();

What does the above do? Is do_something operating on a temporary, t1,
or t2?

Most C++ coders could accurately answer that question but it can still
create confusion. I don't know that I agree with D&D on this but I see
their argument.
 
N

Noah Roberts

Noah said:
They don't want you to be able to do things like so:

Test t1;
Test t2;

// change state of t2...

(t1 = t2).do_something();

Forgot to point out... do_something() is non-const and changes the value
of whatever it's called on.
 
N

Noah Roberts

Alf said:
* Juha Nieminen:

Good point.

I hadn't thought of that, and judging by the almost exlusively T& result
assignment operators around, just about nobody else has either (although
presumably the author of the book the OP mentioned, Mr. Deitel, had).

I think Sutter recommends it as well. I've seen it in one of the red
books (Bjarne's set).
 

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,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top