Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!pasteur!helios.ee.lbl.gov!ux1.lbl.gov!beard From: beard@ux1.lbl.gov (Patrick C Beard) Newsgroups: comp.lang.c++ Subject: Re: Private base class Summary: you need a public or protected in your inherited decl. Message-ID: <4604@helios.ee.lbl.gov> Date: 11 Jan 90 02:15:58 GMT References: <1533@castle.ed.ac.uk> Sender: usenet@helios.ee.lbl.gov Reply-To: beard@ux1.lbl.gov (Patrick C Beard) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 57 X-Local-Date: 10 Jan 90 18:15:58 PST In article <1533@castle.ed.ac.uk> sam@lfcs.ed.ac.uk (S. Manoharan) writes: # #Yet another What's-wrong-with-my-code question: # # 1 class base { # 2 public: # 3 int a; # 4 void f() {} # 5 }; # 6 # 7 class derived: base { # 8 public: # 9 base::a; # 10 base::f; # 11 }; # 12 # 13 # 14 void main() # 15 { # 16 derived x; # 17 # 18 x.a = 0; # 19 x.f(); # 20 } # # #Compiling this with CC (cfront version 1.2.1) gives the following #errors: #CC test.c: #"test.c", line 18: error: a is from private base class #"test.c", line 19: error: f is from private base class #2 errors As it should, you have inherited the base class privately. In other words, all members, public, protected, or private, are inaccessible to the derived class. The fix is to declare derived like so: derived : public base {...}; # #BS says "It is possible to declare some, not all, of the public #members of a base class public memberes of a derived class." #(page 196) Where have I gone wrong then? Could someone help me? # Redeclaring the members a and f can only make the visibility MORE strict, not less. From the AT&T C++ reference manual (pp. 70-71): "If a class is declared to be a base class for another class using the private access specifier, the public and protected members of the base class are private members of the derived class." By not specifying the access in the declaration of the derived class, base is by default private. Hope that helps. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. "..............Good day!" - Paul Harvey - -------------------------------------------------------------------------------