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

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Type System
  5. Message-ID: <1993Jan12.214547.20528@microsoft.com>
  6. Date: 12 Jan 93 21:45:47 GMT
  7. Organization: Microsoft Corporation
  8. References: <1993Jan4.194318.5340@lucid.com> <C0Iy9o.9x@frumious.uucp> <1993Jan9.001948.28388@lucid.com>
  9. Lines: 34
  10.  
  11. In article <1993Jan9.001948.28388@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
  12. >In article <C0Iy9o.9x@frumious.uucp>, pat@frumious.uucp (Patrick Smith) writes:
  13. ||> |    If an lvalue has non-const type,
  14. ||> |    then it refers to a mutable object.
  15. ||> 
  16. ||> There is another loophole. :-(
  17. ||> But it's (I hope) an unusual type of situation. :-)
  18. ||> 
  19. ||>    struct A { A(); /*...*/ };
  20. ||>    A* pa;
  21. ||>    A::A { pa = this; }
  22. ||> 
  23. ||>    void f() {
  24. ||>       const A a;
  25. ||>       // Now pa has non-const type but points to an immutable object.
  26. ||>    }
  27. |
  28. |You're right.  I knew about that loophole, but forgot about it when
  29. |I posted my original item.  If anyone has ideas for a clean way
  30. |to close this loophole, I'd be interested to hear them.  
  31.  
  32. A* pa;
  33.  
  34. A::A() const
  35. {
  36.     pa = this; // compile-time error: non-const ptr to const object
  37. }
  38.  
  39. Such a const ctor can only give its members or base parts values via
  40. the colon section initializers.  Members cannot be changed within the
  41. body of the constructor.  For backwards compatibility, if you don't
  42. declare a const ctor, then the loophole would be allowed to continue
  43. in ctors of that class, and in which case members could continue to
  44. be changed within the body of the ctor.
  45.