home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12741 < prev    next >
Encoding:
Text File  |  1992-08-21  |  2.4 KB  |  73 lines

  1. Xref: sparky comp.lang.c++:12741 comp.std.c++:1101
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!ftpbox!motsrd!news
  4. From: shang@corp.mot.com (David (Lujun) Shang)
  5. Subject: Proposal: Operator <-
  6. Message-ID: <1992Aug21.194134.8614@cadsun.corp.mot.com>
  7. Sender: news@cadsun.corp.mot.com
  8. Reply-To: shang@corp.mot.com
  9. Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
  10. Date: Fri, 21 Aug 92 19:41:34 GMT
  11. Lines: 60
  12.  
  13.  
  14. To provide a transparent generic object pointer (reference), we need a 
  15. new operator <- to clear the side effect caused by operator ->. 
  16. Consider the following example:
  17.  
  18. template <class RefType, class ObjectType>
  19. class REF
  20. {    RefType ref;
  21.    public:
  22.      ObjectType * open ()
  23.           { return (ObjectType*) ref.open(); };
  24.      void close () { ref.close(); };
  25.  
  26.      ObjectType * operator-> () { return open(); };
  27.      ObjectType * operator<- () { return close(); };
  28.      ObjectType   operator*()   { return *open(); };
  29.      REF<RefType,ObjectType> operator++ ()
  30.         { return REF<RefType,ObjectType> rf ={ ref.next(); };
  31.      ...
  32. };
  33.  
  34. The operator <- is always called implicitly by the compiler:
  35.  
  36.      When operator-> followed by a lvalue, operator<- is called 
  37.      after the assignment, otherwise, operator<- is called 
  38.      immediately after the member is read.
  39.  
  40. For example:
  41.  
  42.      REF<PersistentRef, Circle> circle_ref;
  43.      position = circle_ref->position;
  44.  
  45. Operator-> will call PersistentRef's "open" to read the circle object 
  46. stored in a persistent storage, and the operator <- will be called 
  47. after the object is read into the memory.
  48.  
  49. The default operator <- for standard pointer is equivalent an empty 
  50. operation.
  51.  
  52. Without built-in operator <-, the programmer must close the object 
  53. storage explicitly:
  54.  
  55.     position = circle_ref->position; circle_ref.close();
  56.  
  57. If position is a member function:
  58.  
  59.     position = circle_ref->position();
  60.  
  61. you must call close at the end of the function. This may cause another 
  62. problem: position() can not be called by other member functions within 
  63. the object, otherwise the open/close operations will be unbalenced.
  64.  
  65. If operator<- is provided by the language, then we can write programs 
  66. independent to any particular memory management protocol. For example, 
  67. we can write dynamic data linking structures like trees, chains, rings 
  68. that can run both in standard alloc/free memory and in MS Windows heap 
  69. without changing the source.
  70.  
  71. David Shang
  72.  
  73.