home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / cplus / 2021 < prev    next >
Encoding:
Text File  |  1993-01-10  |  4.0 KB  |  124 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!swrinde!network.ucsd.edu!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Type System
  5. Message-ID: <1993Jan10.162757.1097@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1993Jan4.194318.5340@lucid.com> <C0Iy9o.9x@frumious.uucp> <1993Jan9.001948.28388@lucid.com>
  10. Date: Sun, 10 Jan 1993 16:27:57 GMT
  11. Lines: 111
  12.  
  13. In article <1993Jan9.001948.28388@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  14. >In article <C0Iy9o.9x@frumious.uucp>, pat@frumious.uucp (Patrick Smith) writes:
  15. >|> There is another loophole. :-(
  16. >|> But it's (I hope) an unusual type of situation. :-)
  17.  
  18.     Special case of a more general problem?
  19.  
  20. >|> 
  21. >|>    struct A { A(); /*...*/ };
  22. >|>    A* pa;
  23. >|>    A::A { pa = this; }
  24. >|> 
  25. >|>    void f() {
  26. >|>       const A a;
  27. >|>       // Now pa has non-const type but points to an immutable object.
  28. >|>    }
  29.  
  30.     LOVELY! Who needs ~const?  Who needs cast away const either?
  31.  
  32.     struct A { A*me; A() : me(this) {} 
  33.         int i;
  34.         f()const; 
  35.     }
  36.  
  37.     A::f()const { ....     
  38.         me->i=1; // fooled ya!
  39.     };
  40.  
  41. >
  42. >You're right.  I knew about that loophole, but forgot about it when
  43. >I posted my original item.  If anyone has ideas for a clean way
  44. >to close this loophole, I'd be interested to hear them.  
  45.  
  46.     Do we want to close it?
  47. >
  48. >The problem is that the object is only "marked" immutable when the
  49. >constructor returns.  The constructor itself is supposed to be free
  50. >to modify the object in any way it chooses.
  51.  
  52.     The problem is that the object is *never* marked const.
  53. What is made const is the 'reference' or pointer to the object.
  54.  
  55.     If we had 'const' constructors we could at least
  56. distinguish whether a const object was being constructed or not.
  57. Please take below with salt (LOTS):
  58.  
  59.     You could close the loophole by disallowing any use
  60. of 'this' :-)
  61.  
  62.     This also stops the constructor creating another
  63. object and passing it the non-const this.
  64.  
  65.     I'm not sure if the loophole matters though. The 
  66. const mechanism is an aid only. It totally fails to 
  67. handle 'delegation': it is a well known trick for cache
  68. support etc:
  69.  
  70.     class X { int i; .. };
  71.     class H { X* p; H() p(new X){}};
  72.     const H h; // nothing can help us: the constructor of H
  73.            // doesnt know its const
  74.           // so it cant create a 'const' X object
  75.  
  76.     h.p->i=2; // modified the 'object'
  77.  
  78. To get around this
  79.  
  80.     class constH {const X* p; constH() : p(new const X){}};
  81.     constH h;
  82.     h.p->i=1; //error
  83.  
  84. which means whenever you want to support 'const' and 'delegation'
  85. like this you have to create two 'handle' classes : a const one,
  86. and a non-const one. (This doesnt close the loophole, just
  87. lets you create const objects that *subsequently* cant
  88. be modified).
  89.  
  90. I'm not sure even having 'const' constructors would help here.
  91. (Set a switch?)
  92.  
  93. Now: the loophole is just a special case of this in which
  94. the pointer *happens* to refer to the object itself.
  95.  
  96. So it isnt a loophole unless the delegation thing above
  97. is also. The user code is just delegating control to itself :-)
  98.  
  99. Moral: there's no loophole: your notion that there exist
  100. const objects is faulty. Only references and pointers
  101. to objects can be 'const'. Its up to the constructor
  102. to determine if it wants to give certain objects
  103. non-const access to the object by passing them
  104. the 'this' pointer.
  105.  
  106. General public access is still restricted as intended.
  107. Priviledged access is still available to those
  108. trusted objects that the constructor deems to
  109. grant non-const priviledge to (including the object
  110. itself --- as a special case).
  111.  
  112. Now consider a REAL const object:
  113.  
  114.     rom X x;
  115.  
  116. Thats really in ROM. Now: the constructor had better
  117. NOT modify the object: its REALLY immutable!
  118.  
  119. -- 
  120. ;----------------------------------------------------------------------
  121.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  122.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  123. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  124.