Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

17
  • 23
    @kriss: No, you can also get a dangling reference by returning an automatic variable by reference. Commented Nov 2, 2010 at 6:14
  • 20
    @kriss: It's virtually impossible for a compiler to detect in the general case. Consider a member function that returns a reference to a class member variable: that's safe and should not be forbidden by the compiler. Then a caller that has an automatic instance of that class, calls that member function, and returns the reference. Presto: dangling reference. And yes, it's going to cause trouble, @kriss: that's my point. Many people claim that an advantage of references over pointers is that references are always valid, but it just isn't so. Commented Nov 2, 2010 at 13:15
  • 7
    @kriss: No, a reference into an object of automatic storage duration is very different from a temporary object. Anyway, I was just providing a counter-example to your statement that you can only get an invalid reference by dereferencing an invalid pointer. Christoph is correct -- references are not any safer than pointers, a program which uses references exclusively can still break type safety. Commented Nov 2, 2010 at 15:15
  • 11
    References are not a kind of pointer. They are a new name for an existing object. Commented Jul 20, 2011 at 1:28
  • 29
    @catphive: true if you go by language semantics, not true if you actually look at the implementation; C++ is a far more 'magical' language that C, and if you remove the magic from references, you end up with a pointer Commented Jul 23, 2011 at 9:07