TITLE: implementation notes on references (Newsgroups: comp.lang.c++, 28 Aug 99) PHILEMON: Philemon > blargg wrote: > > Nope. References aren't objects, and this does have implications. > I didn't get that. References aren't objects and pointers aren't > objects. What are the implications you had in mind? BECKER: Pete Becker Pointers are objects with a small 'o', in the same way that ints are objects: they occupy storage and there are rules that define the valid operations on those regions of storage. References do not have to occupy storage, and even when they do, there are no operations that the user can perform on that storage. Conceptually, a reference is a name for some object, and nothing else. References are often implemented with pointers, but often they are not. For example; void f(int i) { int &ir = i; printf("%d\n", ir); } This can be implemented without allocating any storage for ir. The compiler just needs to look through the reference to the object it refers to, and pass the value of i to printf. UNKNOWN#1: >>> References are *required* only in copy (ctor and operators.) UNKNOWN#2: >> They are needed for overloading built-in operators, such as those you >> mention, and also other operators (operator +, operator *, etc.) PHILEMON: > No, they are not required there. They _can_ be used, of course, and > syntactically they are a good fit there--but again, it is syntax, and > syntax only. BECKER: Well, in a sense that's true, but such trivializations gloss over important differences. How would you write the equivalent of the following code, preserving the use of overloaded operators, without references? cout << "Hello, " << "world" << endl; Two more important difference between references and pointers are that references always refer to an object and that the object they refer to cannot be changed. The first is a useful guarantee, because you don't need to check for null pointers. The second is also useful because you can cache values without having to worry that the values will be invalidated by changing what object a reference refers to.