Path: utzoo!attcan!uunet!mcsun!ukc!edcastle!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: >this< as a reference Summary: references in C++ are not like in other OO languages. Keywords: references C++ OO Message-ID: <2058@aber-cs.UUCP> Date: 20 Oct 90 17:56:05 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 86 In article <58304@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >Now that there is no need to assign to >this<, wouldn't it have >been cleaner if >this< were a reference, and not a pointer ? I think so, but if one had made "this" a reference [in which case call it "self"] then one should have changed other parts of the language to return references too. For example, in that case "new" should return a reference rather than a pointer too. This looks very bizarre to me. Are you under the impression that references are kind of like pointers? I do not think so. I think they are very different. I guess that the new operator should continue to return pointers even if there are rerefences around. Then, if one is making use of references central in the language, one probably needs to make references assignable, like other OOP languages. Here I think that you are falling prey to nominalism. There are things called references in C++ and in say Simula67 or Eiffel. They are completely different beasts, even if the name is the same. The Simula 67 and Eiffel references are the direct equivalents of pointers in C++. C++ references are like Algol 68 constant references, i.e. aliases. Pointers are another way of doing aliases, but they have one level of indirection more than C++ references (they are references to references). Not using references for everything has given C++ some indirect advantages, though. Most other OOPLs always access objects via reference, using reference semantics. Read "pointers" throughout... Of course C++ has the advantage that to define composite it can use contiguity and not just pointers. This is a *large* advantage, and makes it clear that things like derivation are only justified in the context of a pointer based OO language, which C++ is not. This hinders their ability to offer extended "primitive" classes such as class complex or String classes. In no way I am aware of, frankly. Unless you are speaking of efficiency. But Smalltalk is a pointer based OO language and it does provide fine granularity (not "primitive" -- they are composite) classes, slowly. So, overall, I think the C++ approach has faired pretty well for the language, even if the duality of pointer/reference semantics everywhere leads to some confusion. No confusion at all for those who have studied Algol 68 or BLISS: C++ Algol 68 const int c = 10; INT c = 10; int i := 100; INT i := 100; int &j = i; REF INT j = i; int *p = &i; REF INT p := i; int *p = new int; REF INT p := HEAP INT; Note that three Algol 68 lines above are abbreviations: INT i := 100; REF INT i = LOC INT; i := 100; REF INT p := i; REF REF INT p = LOC REF INT; p := i; REF INT p := HEAP INT; REF REF INT p = LOC REF INT; p := HEAP INT; Explanation: '=' in Algol 68 defines a constant. 'i' above is a constant of type 'reference to integer', and its value is a reference (returned by the local reference allocator LOC) to some memory place. ':=' in Algol 68 is an abbreviation for '=' (when in a declaration), and adds an implicit extra REF and a call to the local reference generator. In a sense in C all declarations imply one REF and LOC, except that 'const' takes away the ref, '&' takes way the LOC, and '*' adds an extra REF. Algol 68 C++ TYPE x = value; const TYPE x = value; REF TYPE x = LOC TYPE; TYPE x; REF TYPE x = y; TYPE &x = y; REF REF TYPE x = LOC REF TYPE; TYPE *x; -- Piercarlo "Peter" Grandi | ARPA: pcg%uk.ac.aber.cs@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk