Topic: Trusting references (was: Public const feature)


Author: Steve Clamage <stephen.clamage@eng.sun.com>
Date: 1997/09/04
Raw View
Michael Corbeil wrote:
>
> I have read the two O'Reilly books on C++ and part way through Bjarne
> Stroustrup's second edition on C++ and don't recall seeing anywhere, nor
> ever having learned in a C++ programming course that references cannot
> be modified after they've been initialized.

In C++PL 2nd edition, look in the index under "reference". The first
page lised is p61.

Page 61, section 2.3.10 "References" :
"Consequently, the value of a reference cannot be changed after
initialization; it always refers to the object it was initialized
to denote."

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Bjorn Fahller <ebcbear@ebc.ericsson.se>
Date: 1997/09/04
Raw View
Michael Corbeil wrote:

> Where am I or the others
> going astray, here, because as far as I know, a reference is not
> constant until it has been qualified or declared as such, explicitly?

It's a misunderstanding. The discussions about references being const
refers to the reference itself, rather than how it treats what it refers
to. The reference itself can be interpreted as a constant, since there
is no way to make it refer to something else than it was initiated with.
Once bound to refer to a variable, it will refer to that variable as
long as the reference is valid.
   _
/Bjorn.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Tom Payne <thp@cs.ucr.edu>
Date: 1997/09/04
Raw View

In comp.std.c++ Michael Corbeil <mcorbiel@worldnet.att.net> wrote:
: I have read the two O'Reilly books on C++ and part way through Bjarne
: Stroustrup's second edition on C++ and don't recall seeing anywhere, nor
: ever having learned in a C++ programming course that references cannot
: be modified after they've been initialized.  Where am I or the others
: going astray, here, because as far as I know, a reference is not
: constant until it has been qualified or declared as such, explicitly?

Let's distinguish between (a) modifying (reseating) a reference and
(b) modifying its referrent:

   (a)  The omission of reseatability of references was a conscious
        design decision by Stroustrup because of his negative experiences
        with reseatable references in Algol68 [D&E p. 86].

   (b)  Per the standard you are entirely correct that the referent is
        modifiable unless otherwise specified.  But, to enhance program
        maintainability, many authors (e.g., Alan Holub, "Enough Rope to
        Shoot Yourself in the Foot," I think) advise against non-const
        reference parameters.  This is a style rule, not a language rule.
        The idea is that if all parameters that are subject to modification
        are passed by pointer, there will be a syntactic indication of
        possibile modification at the point of call.  (FWIW, I don't
        subscribe to this rule.)

Tom Payne
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/08/21
Raw View
I wrote:
>> But what we want is something that doesn't require any extra
>> overhead; the solution above adds another word (a reference) to the class
>> size.  (Also, I don't trust references in general.)

Dan Muller  <danm@ziplink.net> asked:
> What don't you trust about references? I use them frequently, and haven't
> run into anything particularly tricky about their application.

In particular, I mistrust their lack of referential integrity.
For example:

    class Foo
    {
    public:
                Foo(Bar &item) : bp(item) { }
    private:
        Bar &   bp;     // A reference
    };

    void xyzzy()
    {
        Bar *   b;
        Foo *   f;

        b = new Bar;
        f = new Foo(*b);

        delete b;       // Whoops!
        b = 0;
    }

The annoying thing about references in C++ is that they aren't the same
critters as references in other languages like Java, Eiffel, and Smalltalk.
In particular, they're permanent - once you initialize a reference, it can
never change.  Kind of like a '*const' pointer.  Which is bad if the object
you're referencing is deleted.  There is no such concept as a null or void
reference in C++.  But there is the very real danger of dangling references.
And the danger is more severe when objects are destructed during exception
unwinding.  (If you could code 'bp = void' then it wouldn't be a problem.)

I only use references when I have to (such as in copy constructors).
Otherwise, pointers suit my needs just fine.  And they can be set to null.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]