Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!wuarchive!brutus.cs.uiuc.edu!psuvax1!rutgers!cmcl2!acf5!sabbagh From: sabbagh@acf5.NYU.EDU (sabbagh) Newsgroups: comp.lang.c++ Subject: Re: Modifying References Message-ID: <936@acf5.NYU.EDU> Date: 28 Nov 89 14:49:32 GMT References: <4280@cadillac.CAD.MCC.COM> Distribution: na Organization: New York University Lines: 33 In-reply-to: dsouza@mcc.com [Desmond D'Souza]'s message of 27 Nov 89 19:02:05 GMT >Summary: Why cannot a reference be changed? As I understand it, references were introduced to C++ as a "correction" to C. Its primary purpose is to minimize copying of objects onto the stack; this can be done with C using pointers, but this has the problem that the _caller_ must know to take the address, resulting in maintainability problems. > e.g. a CAR object contains a reference to an ENGINE object. > a REPAIR may share a reference to the same ENGINE. > ... > e.g. Suppose at some point I need to change the engine in my car: I am > forced to resort to using pointers to an ENGINE (and possibly have to > modify code to conform to the change). > > Or, suppose I simply cannot commit to an engine at the time the car is > constructed, and only do so at some later time. Not to start a flame war, but pointers _are_ the correct solution to this problem, from a semantic point of view. In the above CAR, REPAIR example, it is important to note that you are not only referring to an ENGINE, but your are also (indirectly) REPAIRing the CAR containing the ENGINE. In this sense, pointers and references are the same, and C++ supports extensive pointer semantics. Hadil G. Sabbagh E-mail: sabbagh@csd27.nyu.edu Voice: (212) 998-3285 Snail: Courant Institute of Math. Sci. 251 Mercer St. New York,NY 10012 186,282 miles per second -- it's not just a good idea, it's the law!