home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16701 < prev    next >
Encoding:
Internet Message Format  |  1992-11-21  |  2.2 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!att!allegra!alice!bs
  2. From: bs@alice.att.com (Bjarne Stroustrup)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Confusion on :: and protected (Simple?)
  5. Message-ID: <24266@alice.att.com>
  6. Date: 21 Nov 92 14:27:30 GMT
  7. Article-I.D.: alice.24266
  8. References: <mike.721669287@sdd.comsat.com> <rmartin.722274669@thor>
  9. Distribution: na
  10. Organization: AT&T Bell Laboratories, Murray Hill NJ
  11. Lines: 61
  12.  
  13.  
  14.  
  15. rmartin@thor.Rational.COM (Bob Martin @ Rational) writes
  16.  
  17.  > mike@sdd.comsat.com (Mike Stennett) writes:
  18.  > 
  19.  > |I tried to compile with both DEC's cxx 1.0 and GNU's g++ 2.2.2 but got 
  20.  > |the following errors:
  21.  > 
  22.  > |[39] cxx test.C
  23.  > |test.C:24: error: In this statement, "foo::print" is not accessible.
  24.  > |Compilation terminated with errors.
  25.  > |[40] g++ test.C
  26.  > |test.C: In function `int  static joe::something (int)':
  27.  > |test.C:24: method `int  static foo::print (int)' is protected
  28.  > 
  29.  > |class foo 
  30.  > |{
  31.  > |protected:
  32.  > |    static int print(const int y) { cout << y << endl; return 1;}
  33.  > |};
  34.  > 
  35.  > |class  joe : public foo
  36.  > |{
  37.  > |public:
  38.  > |   static int something(int z);
  39.  > |};
  40.  > 
  41.  > |int joe::something(int z)
  42.  > |{
  43.  > |    foo::print(5);
  44.  >      ^^^^^^^^^^^^^^^    This is the line with the error.
  45.  > |}
  46.  > 
  47.  > ARM 11.5 (p253) says that member functions of derived classes can only
  48.  > access protected members of base classes "through a pointer to,
  49.  > reference to, or object of the derived class (or any class derived
  50.  > from that class). 
  51.  
  52. Actually, the ARM specifically says NONSTATIC member. That example is
  53. perfectly legal, so he seems to have found bugs in those compilers.
  54. On cfront derivatives - and I suspect most C++ compilers - this works
  55. as written. 
  56.  
  57.  > However, you can do this:
  58.  > 
  59.  >     this->foo::print(5); 
  60.  > 
  61.  > This is allowable since you are accessing foo::print through a pointer
  62.  > to a joe (this).  Wierd huh?
  63.  
  64. Very, had the first explanation been true. Fortunately it wasn't.
  65.  
  66. Actually, because joe::something was static there is no `this' pointer
  67. to use for qualification so ``this->foo::print(5)'' would be an error.
  68.  
  69.  > E&S try to justify this in the last paragraph of page 254.  
  70.  
  71. No. The discussion in 11.5 relates to something completely different.
  72.  
  73.     - BJarne
  74.