[comp.lang.c++] Private base class

sam@lfcs.ed.ac.uk (S. Manoharan) (01/10/90)

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

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?

Thanks in advance.

beard@ux1.lbl.gov (Patrick C Beard) (01/11/90)

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.  ".......<dead air>.......Good day!" - Paul Harvey  -
-------------------------------------------------------------------------------

peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (01/11/90)

In article <4604@helios.ee.lbl.gov> beard@ux1.lbl.gov (Patrick C Beard) writes:
>In article <1533@castle.ed.ac.uk> sam@lfcs.ed.ac.uk (S. Manoharan) writes:
>#Yet another What's-wrong-with-my-code question:
># [code sample deleted]
>#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:

[ "fix" deleted]

NO, NO, NO! Now the confusion is perfect! The example should work and does
work under 2.0!!!!! Apperently there is a bug in 1.2.1

I do this all over the place in my code. 

>#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?

You have not gone wrong. That is the correct quote and your interpretation is
correct.

>Hope that helps.

Sorry, no it doesn't!

>-  Patrick Beard, Macintosh Programmer                (beard@lbl.gov) -


Peter
peter@media-lab.media.mit.edu

beard@ux1.lbl.gov (Patrick C Beard) (01/11/90)

In article <4604@helios.ee.lbl.gov> I write:
[code example deleted]
##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.
#
#derived : public base {...};
#
#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.

And, on the next page of the manual (sorry I didn't see this at the time of
my last posting):

class B {
public: int a;
private: int b;
protected: int c;
};

class D : private B {
public:
	B::a;	// make 'a' a public member of D.
	B::b;	// make 'b' a public member of D (which is an error).
protected:
	B::c;	// make 'c' a protected member of D (ok).
	B::a;	// make 'a' a protected member of D (error, reducing access).
};

These are called "access declarations."  Sorry for my confusion.

-------------------------------------------------------------------------------
-  Patrick Beard, Macintosh Programmer                        (beard@lbl.gov) -
-  Berkeley Systems, Inc.  ".......<dead air>.......Good day!" - Paul Harvey  -
-------------------------------------------------------------------------------

ark@alice.UUCP (Andrew Koenig) (01/11/90)

In article <1533@castle.ed.ac.uk>, sam@lfcs.ed.ac.uk (S. Manoharan) writes:

> Yet another What's-wrong-with-my-code question:

[example deleted]

> Compiling this with CC (cfront version 1.2.1) gives the following
> errors:

> "test.c", line 18: error:  a is from private base class
> "test.c", line 19: error:  f is from private base class

It's a bug in 1.2.1, fixed in cfront 2.0
-- 
				--Andrew Koenig
				  ark@europa.att.com

sam@lfcs.ed.ac.uk (S. Manoharan) (01/11/90)

In a previous article I wrote:

#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

Changing line 7 of the above code to:
        7  class derived: private base {
			  ~~~~~~~
results in a correct compilation.

I thought `class D: B {' and class D: private B {' are always
identical. Or so says BS.

Still baffled.

Manoharan.

dog@cbnewsl.ATT.COM (edward.n.schiebel) (01/11/90)

From article <4604@helios.ee.lbl.gov>, by beard@ux1.lbl.gov (Patrick C Beard):
> In article <1533@castle.ed.ac.uk> sam@lfcs.ed.ac.uk (S. Manoharan) writes:
> #
> #Yet another What's-wrong-with-my-code question:
> #
> # (deleted example code)
> #
> #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. 
Not exactly. By default the members are inaccessible to USERS of the 
derived class. Member functions of the derived itself retains access to 
base's public and protected.  By specifying base::a and base::f in
derived's public section, these members of base have now been declared
as public to derived and accessible to users of derivec.  The given
example code does compile correctly using cfront 2.00.

> The fix is to declare derived like so:
> 
> derived : public base {...};
> 
Not if you really wanted private derivation.
> #
> #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.

You seem to be confusing the default classification of base's member data
with what you are allowed to do when adjusting access.  The code in
the original posting is correct.  What you cannod to is:

class base {
public:
	int a;
};

class derived : public base {
private:
	base::a;
};

Here you are trying to take away access granted by default and will
get the error message (from cfront 2.00):

 error:  public member base::a specified private

	Ed Schiebel
	AT&T Bell Laboratories
	dog@vilya.att.com
			

comeau@utoday.UUCP (Greg Comeau) (01/12/90)

In article <1533@castle.ed.ac.uk> sam@lfcs.ed.ac.uk (S. Manoharan) writes:
>class base { public: int a; void f() {} };
>class derived: base { public: base::a; base::f; };
>void main() { derived x; x.a = 0; x.f(); }
>Compiling cfront version 1.2.1 gives "error: a is from private base class",
>...Where have I gone wrong then? Could someone help me?
>

I think you're stuck with a 1.2.1 nuance.

Your code should work fine with a 2.0 cfront.
-- 
Greg, Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418
Producers of CC C++, SysAdm columnist for UNIX Today!, Microsoft Systems Journal
(C programming), + others. Also, BIX c.language & c.plus.plus conf. moderator.
Here:attmail!csanta!greg / BIX:comeau / CIS:72331, 3421 / voice:718-849-2355