Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!ames!amdahl!rtech!ingres!llama!jas From: jas@llama.Ingres.COM (Jim Shankland) Newsgroups: comp.lang.c++ Subject: Re: Why use reference type over pointer type? Message-ID: <1990Aug2.234350.6507@ingres.Ingres.COM> Date: 2 Aug 90 23:43:50 GMT References: <1676@dinl.mmc.UUCP> <1806@cs.rit.edu> Reply-To: jas@llama.Ingres.COM (Jim Shankland) Organization: The Eddie Group Lines: 48 In article <1806@cs.rit.edu> jeh@cs.rit.edu writes: >From article <1676@dinl.mmc.UUCP>, by noren@dinl.uucp (Charles Noren): >> Its obvious, I'm new to C++. >> What does does a reference type give me over a pointer type >> that I've come to love and know well in C? ...... > >I, as a professor, have been asked this so many times that I would love >to hear people's opinions on this. > >I personally prefer to use them because I like > > call-by-reference semantics > "lvalue" - type variables It's not clear to me what you mean by these two that can't be had by explicitly using pointer variables. One place where references come in handy is when you define an operator on a class, and you want the arguments to be passed by reference. I.e., given a class Huge, with Huge h, i, and j, you can define "friend Huge& operator+(Huge&, Huge&)", and thus say "h = i + j" while still passing i and j to the operator function by reference. Consider this puzzle: suppose that assignment and initialization semantics in C++ were altered such that referencing and dereferencing were performed implicitly as needed. That is, given: X x, *xp; you could say: x = xp; // rhs implicitly converted to *xp xp = x; // rhs implicitly converted to &x just as you can currently say: int i, double d; ... d = i; // i implicitly converted to (double) i Some 60's language -- Algol 68? -- did this. *Now* are references ever needed? Note that the operator+ example above could now be coded as "friend Huge *operator+(Huge *, Huge *)". As near as I can tell, this makes the need for references disappear altogether. I'd certainly be interested if anyone can prove me wrong .... jas