peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (09/19/89)
// I want `another' to have access to the private part of `derived'
// which, under cfront 1.2, I can apparently only get by making `another' friend
// to the base class.
// this program produces:
// make test
// CC -O -n -o test test.C
// CC test.C:
// "test.C", line 32: error: a is protected
// 1 error
// *** Error code 1
// Stop.
#include <stream.h>
class base{
protected:
int a;
public:
base() : a( 1 ) {}
// friend class another; this is what it takes to work. Why?
};
class derived : private base{
public:
// friend int another::mult(); this is really what I want
friend class another; // but I'd settle for this
derived() : () {}
};
class another{
int b;
public:
another() : b( 1 ) {}
int mult( derived& d )
{ return b * d.a; } // I need it here
};
main()
{
another a;
derived d;
cout << a.mult( d ) << "\n";
}
// Notice that this is simplified considerably from the actual problem where I
// really need the friend mechanism, rather then a function such as
// int derived::geta() { return a; }
// which obviously would also solve my problem.
// What am I overlooking?
// Thanks for your help!
// Peter
// peter@media-lab.media.mit.edu
*/shopiro@alice.UUCP (Jonathan Shopiro) (09/19/89)
The summary says it all. Under C++ 2.0, friend functions have exactly the same access rights as member functions of the same class. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229