Topic: Language design question: pointer vs. reference
Author: Ari.Huttunen@hut.fi (Ari Juhani Huttunen)
Date: 23 Aug 91 23:22:26 GMT Raw View
In article <nicwi.682932811@isy.liu.se> nicwi@isy.liu.se (Niclas Wiberg) writes:
>However, reference types has a significant drawback compared to
>pointers: they cannot be made to reference an other object than
>the one it was initialized with.
>In my view, this is a major drawback of C++.
I share this view. As I understand it the reason why references cannot
be made to refer to anything else are syntactic. Correct me if I am wrong.
Someone(s) already proposed that a different assignment operator would
be used if a reference would be made to refer to something else as opposed
to copying the value. I will now show you how this can be done _without_
creating a new assignment operator.
Assume these definitions. (Initializations are not shown for clarity.)
int i,j;
int *iptr, *jptr;
int &iref, &jref;
iptr = jptr; // Make iptr and jptr point to the same location.
// Data flow: int*
*iptr = *jptr; // Make the contents of the locations pointed to by
// iptr and jptr identical.
// Data flow: int
iref = jref; // Make the contents of the locations refered to by
// iref and jref identical.
// Data flow: int
&iref = &jref; // Make iref and jref point to the same location.
// Data flow: int*
The last assignment means that the address of jref is assigned to the
address of iref. No ambiguity can result because iref is an lvalue and
assigning to an address of an lvalue is not (yet) allowed.
I think these assignment statements fit together well. The default
behaviour of assigning int*'s is to change _where_ the variables point.
This can be changed by prefixing them with '*', in which case _what_ they
point to is changed.
The default behaviour of assigning int&'s is to change _what_ the variables
refer to. This can be changed by prefixing them with '&', in which case
_where_ they refer is changed.
Comments?
--
...............................................................................
Ari Huttunen Ari.Huttunen@hut.fi I{-R'lyeh! Cthulhu fhtagn! I{! I{!
90-7285944
Author: mvm@caesun1.uucp (Matt Mahoney)
Date: 4 Sep 91 20:06:43 GMT Raw View
In article <ARI.HUTTUNEN.91Aug24012226@zelda.hut.fi> Ari.Huttunen@hut.fi
(Ari Juhani Huttunen) writes:
>In article <nicwi.682932811@isy.liu.se> nicwi@isy.liu.se (Niclas Wiberg) writes:
>
>>However, reference types has a significant drawback compared to
>>pointers: they cannot be made to reference an other object than
>>the one it was initialized with.
>int &iref, &jref;
>&iref = &jref; // Make iref and jref point to the same location.
> // Data flow: int*
>
>The last assignment means that the address of jref is assigned to the
>address of iref. No ambiguity can result because iref is an lvalue and
>assigning to an address of an lvalue is not (yet) allowed.
This would not work in general. Consider:
class foo {
public: int& operator&();
};
void f(foo &iref, foo &jref)
{
&iref = &jref; // Data flow: int, NOT foo*
}
References are a syntactic convenience. For most programs, it is
not a great burden to have them be constant. There is no need to change
C++ to do something that you can already accomplish by using pointers.
--------------------------------
Matt Mahoney, mvm@epg.harris.com
#include <disclaimer.h>