ngo@flash.harvard.EDU (Thomas Ngo) (07/30/90)
How does a reference differ from a const pointer to a const object (besides the obvious need to dereference the latter)? I hope this question is in the spirit of this newsgroup.
rfg@NCD.COM (Ron Guilmette) (08/01/90)
In article <3679@husc6.harvard.edu> ngo@tammy.harvard.EDU (Thomas Ngo) writes: > > How does a reference differ from a const pointer to a const > object (besides the obvious need to dereference the latter)? > > I hope this question is in the spirit of this newsgroup. As you note, the first difference is that references are implicitly dereferenced (in almost all contexts - including sizeof). Also, since you cannot change the value of a reference (once it has been initialized) it seems that references are (in this respect) very much like const pointers. Note however that the referent object of a reference could be either const or non-const, just as the referent of a pointer variable could be. Some differences that come to mind are: 1) Pointers to pointers are allowed. Pointers to references are not. 2) References to pointers are allowed. References to references are not. 3) Arrays of pointers are allowed. Arrays of references are not. (At least the last time I asked Bjarne, he said that he put that in the language but then decided that it was a mistake and that it ought to be taken out again.) 4) Given the declarations: extern int array[], func(); and the function call: foobar (array, func); the following possible declarations for foobar() will "match": extern void foobar (int* const arg1, int (* const arg2)()); it is not at all clear however (to me anyway) whether or not the following declaration would also "match" the call above: extern void foobar (int& arg1, int (& arg2)()); I'm sure that there are more, but I have forgotten them. -- // Ron Guilmette // C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.