Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!milano!cadillac!sunburn!dsouza From: dsouza@mcc.com [Desmond D'Souza] Newsgroups: comp.lang.c++ Subject: Modifying References Message-ID: <4280@cadillac.CAD.MCC.COM> Date: 27 Nov 89 19:02:05 GMT Sender: news@cadillac.CAD.MCC.COM Reply-To: dsouza@mcc.com Distribution: na Organization: MCC VLSI-CAD Program, Austin, TX Lines: 63 Summary: Why cannot a reference be changed ? Firstly, I LIKE "reference" types. Besides function call/return efficiency, I like true call by reference semantics, without having to mess with pointers and de-referencing. I also like being able to share references to the same object without treating the reference as a pointer. e.g. a CAR object contains a reference to an ENGINE object. a REPAIR may share a reference to the same ENGINE. I do not understand this restriction: [B.S. "The C++ Prog.Lang", p.56] "A Reference always refers to the object it was initialized to denote" If references are implemented by pointers, why should a reference not be able to be associated with different objects at different times ? If this was possible one could use references in a lot of other places, and save pointers for things like pointer arithmetic. 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. Solutions: 1. Use pointers: the only current option 2. Permit references to change (i.e. define some operations besides initialization, on "references" themselves) : We would need to do the following to references: 1. check if they are currently associated with an object: like a void pointer check 2. re-associate a reference: like a pointer assignment 3. possibly, sever an association: like assigning NULL to pointer Problem: Cannot use "=" , since the assignment operator has a pre-defined (or overloaded) meaning for the referred-to class. Solution: Since all reference types would need this, and existing operators -- overloaded or not -- should operate on the referred-to object, maybe use some existing keywords ? MyClass& ref; MyClass obj; ref.void() ; // like: Check (ptr == 0) ref.switch(obj) ; // like: pointer assignment (ptr = &obj) ref.break() ; // like: NULL assignment (ptr = 0) Maybe member functions should not be used, but you get the idea. Of course, there may be some quite profound reason for the restriction which I have missed completely. Desmond. Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza