[net.lang.c++] some small questions

gjditchfield@watrose.UUCP (Glen Ditchfield) (03/18/86)

Can some kind soul tell me how "real" C++ handles these problems? We use a
buggy pre-release here, so I can't just try them.
- Does C++ make some distinction between ++x and x++, where x is of a user-
  defined class? If operator++() is written to return the incremented value,
  C++ could handle x++ by saving x, calling ++, and passing on the saved value.
- Does "void fred( gorf& const g)" make syntactic sense? What I am trying to
  say is that fred uses a reference to the actual parameter, _but_can_not_
  _alter_it_. This is like the CONST parameter attribute that some Pascal
  dialects support.
These questions are not to be interpreted as suggestions for extensions to the
language; I would hate to see C++ crushed by "creeping featurism".
-----
None of the opinions above are the product of more than 10 second of thought.

bs@alice.UucP (Bjarne Stroustrup) (03/19/86)

> From: gjditchfield@watrose.UUCP (Glen Ditchfield)
> Subject: some small questions
> Organization: U of Waterloo, Ontario
> 
> Can some kind soul tell me how "real" C++ handles these problems? We use a
> buggy pre-release here, so I can't just try them.

Get a proper release then (I hate seing my old bugs after I have managed to
make the fixes made available).

> - Does C++ make some distinction between ++x and x++, where x is of a user-
>   defined class?

No.

> If operator++() is written to return the incremented value,
>   C++ could handle x++ by saving x, calling ++, and passing on the saved value.

Too subtle.
The compiler cannot even assume that an overloaded ++ is an increment operation.

> - Does "void fred( gorf& const g)" make syntactic sense? What I am trying to
>   say is that fred uses a reference to the actual parameter, _but_can_not_
>   _alter_it_. This is like the CONST parameter attribute that some Pascal
>   dialects support.

Yes, but ``&const'' means ``constant reference'' and every reference is a constant
anyway since you cannot change its ``value''; that is, you cannot make it denote
a different object. Had there been a re-initialization operation for references
it would have made sense.

Anyway, what you were trying to seay was ``reference to constant object'' not
``constant reference to object''. This is how:

	fred(const gorf& g);

I am personally nervous about programs where reference arguments are used
extensively and wished I had used ``reference to constant'' systematically
in the book. Arguments of ``reference to constant'' type provide a safe
and efficient way of passing large chunks of information; like ``by value'',
but with less copying.

	- Bjarne Stroustrup (AT&T Bell Labs, Murray Hill)