home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16224 < prev    next >
Encoding:
Text File  |  1992-11-14  |  1.8 KB  |  72 lines

  1. Path: sparky!uunet!ukma!darwin.sura.net!dtix!mimsy!sdd.comsat.com!mike
  2. From: mike@sdd.comsat.com (Mike Stennett)
  3. Newsgroups: comp.lang.c++
  4. Subject: Confusion on :: and protected (Simple?)
  5. Message-ID: <mike.721669287@sdd.comsat.com>
  6. Date: 13 Nov 92 15:41:27 GMT
  7. Sender: usenet@sdd.comsat.com (USENET News Admin)
  8. Distribution: na
  9. Organization: SDD, COMSAT Labs, Clarksburg, MD
  10. Lines: 60
  11.  
  12. This is probably a simple question.  I've looked in my manuals and
  13. read the FAQ and I don't see why the following program does not work.
  14.  
  15. Can anyone tell me why?  (This is obviously a simplified example.)
  16. Why does the :: operator interfere with the derived class accessing
  17. the protected function?
  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.  
  30. #include <stddef.h>     // For "size_t" type
  31. #include <iostream.h>    // For stream I/O
  32.  
  33. class foo 
  34. {
  35. public:
  36.    int x;
  37.  
  38. protected:
  39.     static int print(const int y) { cout << y << endl; return 1;}
  40. };
  41.  
  42. class  joe : public foo
  43. {
  44. public:
  45.    static int something(int z);
  46. protected:
  47.      static int print(const int y) { cout << "what" << y << endl; return 1;}
  48.  
  49. };
  50.  
  51. int joe::something(int z)
  52. {
  53.     foo::print(5);
  54.     cout << "Did it." << endl;
  55.     return 1;
  56. }
  57.  
  58. main()
  59. {
  60.     joe xyz;
  61.     
  62.     cout << "Starting Program" << endl;
  63.     xyz.x = 4;
  64.     xyz.something(4);
  65.     cout << "Ending Program" << endl;
  66. }
  67.  
  68. --
  69. Signed: Mike Stennett           Domain: mike@sdd.comsat.com
  70. Phone: +1-301-428-4083          Fax: +1-301-428-7747
  71. USPS: COMSAT Labs, 22300 COMSAT Drive, Clarksburg, MD 20871
  72.