Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confusing passage in C++ book

62 views
Skip to first unread message

Paul

unread,
Jun 12, 2015, 5:13:37 PM6/12/15
to
The passage quoted below seems to say that a=b=c; is only allowed if assignment returns a reference. This doesn't seem correct (to me). Assignment is right-associative. Even if operator = was written to return by value, it seems (to me) that a = b = c; would be fine. So I don't get why he says that you need to return a reference to allow chained assignments. I do agree that you need to return a non-const reference to be able to write (a = b) = c;

Thanks for your help,

Paul

BEGIN QUOTE
For IntCell, the signatures of these operations are
~IntCell( ); // Destructor
IntCell( const IntCell & rhs ); // Copy constructor
IntCell( IntCell && rhs ); // Move constructor
IntCell & operator= ( const IntCell & rhs ); // Copy assignment
IntCell & operator= ( IntCell && rhs ); // Move assignment
The return type of operator= is a reference to the invoking object, so as to allow chained assignments a=b=c. Though it would seem that the return type should be a const reference, so as to disallow nonsense such as (a=b)=c, that expression is in fact allowed in C++ even for integer types. Hence, the reference return type (rather than the const reference return type) is customarily used but is not strictly required by the language specification.
END QUOTE
Message has been deleted

Victor Bazarov

unread,
Jun 12, 2015, 5:29:18 PM6/12/15
to
You're correct. The non-const reference return was actually to allow
calling a member function on the result of assignment, I think,
something like

(b = c).foo();

The idea was that such code should be equivalent to

b = c;
b.foo();

And, if 'b' was possible to assign to, then it should be possible to
call a non-const member function in the same scope, hence it should be
possible to combine those in a single expression. If the assignment
returned some other type, calling a non-const member would be
problematic, I think.

V
--
I do not respond to top-posted replies, please don't ask

Victor Bazarov

unread,
Jun 12, 2015, 5:34:58 PM6/12/15
to
On 6/12/2015 5:19 PM, Stefan Ram wrote:
> Paul <peps...@gmail.com> writes:
>> Confusing passage in C++ book
>
> When you write a subject line for a post, you should use a
> subject that describes the technical topic of your question.

Yeah! You tell him, Stefan! It ought to be a subject like like "Quick
simple question:" or "typename" or "some experiments with type names"

There is a Russian saying "Чья бы корова мычала..."; I think Americans
use the expression "You're a fine one to talk!". Look it up!

>
> For example, here a good subject would be:
>
> »the value of the assignment operator«.
>
> Don't use uninformative broad subjects, such as
>
> »I read this in a book«,
> »I have a C++ question«,
> »A question about the C++ language«, or
> »Here's another question from me«.
Message has been deleted
Message has been deleted

Victor Bazarov

unread,
Jun 12, 2015, 7:15:24 PM6/12/15
to
On 6/12/2015 5:40 PM, Stefan Ram wrote:
> Victor Bazarov <v.ba...@comcast.invalid> writes:
>> On 6/12/2015 5:19 PM, Stefan Ram wrote:
>>> Paul <peps...@gmail.com> writes:
>>>> Confusing passage in C++ book
>>> When you write a subject line for a post, you should use a
>>> subject that describes the technical topic of your question.
>> Yeah! You tell him, Stefan! It ought to be a subject like like "Quick
>> simple question:" or "typename" or "some experiments with type names"
>
> Today is the subject-line awareness day.
>
> To raise the awareness of the importance of technically
> specific subject lines, I indeed have posted some posts with
> an intentionally uninformative subject line »Quick simple
> question:«. Possibly, I will not continue this beyond this
> subject-line awareness day.

Lame! I encounter this every day on the road. If some a$$#ole feels
offended by your mere presence on the same road, or just wants to show
his (it's most often males) imagined superiority, they will try to cut
you off, spray the windshield washer fluid when you are behind them and
behave in some other obnoxious way to "learn you a lesson". Don't
behave like those idiots. Give a *good example* and *refrain from
preaching*. You haven't earned the right to preach, especially after
you have not practiced when you're about to preach. OK?

Victor Bazarov

unread,
Jun 12, 2015, 7:20:51 PM6/12/15
to
On 6/12/2015 5:53 PM, Stefan Ram wrote:
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> Victor Bazarov <v.ba...@comcast.invalid> writes:
>>> On 6/12/2015 5:19 PM, Stefan Ram wrote:
>>>> Paul <peps...@gmail.com> writes:
>>>>> Confusing passage in C++ book
>>>> When you write a subject line for a post, you should use a
>>>> subject that describes the technical topic of your question.
>>> Yeah! You tell him, Stefan! It ought to be a subject like like "Quick
>>> simple question:" or "typename" or "some experiments with type names"
>> Today is the subject-line awareness day.
>> To raise the awareness of the importance of technically
>> specific subject lines, I indeed have posted some posts with
>> an intentionally uninformative subject line »Quick simple
>> question:«. Possibly, I will not continue this beyond this
>> subject-line awareness day.
>
> To explain this better, I would like to remind the
> newsgroup that on April 20, 2015, I already posted this:
>
> |Newsgroups: comp.lang.c++
> |Subject: Re: Quick Question
> |From: r...@zedat.fu-berlin.de (Stefan Ram)
> |
> |Doug Mika <doug...@gmail.com> writes:
> |>Subject: Quick Question
> |
> | You should use more specific subject lines in Usenet.
> |
>
> But the person addressed was not willing to consider
> this suggestion on subsequent posts.
>
> Instead we still get more and more subjects lines,
> like, recently: »Quick simple question:«.

And? Instead of explaining what you mean, you start mimicking them
(supposedly in hopes that they notice how absurd such behaviour is and
will understand all by themselves that you're trying to show them how to
do by doing the exact opposite). They don't care! All you do is look
ridiculous yourself. Whatever your intention is, however good it might
be, it is only known to *you*. Don't presume that everybody thinks the
same or sees/notices the same aspects of life.

Paul N

unread,
Jun 14, 2015, 5:39:05 PM6/14/15
to
I think you're reading a bit too much into it. If operator= returned, say, void, then you wouldn't be able to chain assignments. So you need to do something else. Returning a reference is one possibility, but I don't think the passage actually says that it's the only possible one. Returning a reference is more usual than returning a value as it is likely to be more efficient.

Paul

unread,
Jun 15, 2015, 1:26:20 PM6/15/15
to
Thanks.

Yes, that's what the author meant. He also accepts my criticism of the wording, and will be clearer in future editions.

Paul
0 new messages