Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!teknowledge-vaxc!mkhaw From: mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) Newsgroups: comp.lang.c++ Subject: Re: accessing base class protected members Message-ID: <26705@teknowledge-vaxc.ARPA> Date: 15 Feb 89 21:44:35 GMT References: <1000008@hpclscu.HP.COM> Organization: Teknowledge, Inc., Palo Alto CA Lines: 73 <1000008@hpclscu.HP.COM>, by shankar@hpclscu.HP.COM (Shankar Unni): >> class higher : public bottom > ^^^^^^ >> { >> protected: >> float hifloat; >> >> public: >> float gethi() { return hifloat; } >> >> friend void mumble(higher&); >> }; > > The [compiler error] message *is* correct. > > Since the "public" qualifier is used in the inheritance, the members of > "higher" can only access the public members of "bottom". This restriction > also carries over to any friends of the derived class. > > What Mike needs to do to make this do what he wants is to declare "higher" > as: > > class higher : bottom { > ... > } That's not the obvious interpretation. I quote from Stroustrup (Chapt. 7, 7.2.3: Visibility): class manager : public employee { // ... }; This means that a public member of class _employee_ is also a public member of class manager. ... ... ... one can declare a _private_ base class by simply leaving out the word _public_ in the class declaration: class manager : employee { // ... }; This means that a public member of the class _employee_ is a PRIVATE MEMBER [emphasis added] of class _manager_... Friends of a derived class have the same access to base class members as do member functions. In Stroustrup's paper "An Overview of C++", in the section titled "Visibility Control", it says about _protected_ that: a member should be private as far as functions outside the class hierarchy are concerned but accessible to member functions of a derived class in the same way that it is accessible to members of its own class. Such a member is said to be _protected_. And in "What is Object-Oriented Programming?", in "Encapsulation" section, he says: Friend functions have access to private and protected members just like member functions. So I concluded that a friend of a derived class should be able to access a protected member of a base class as though it were a member of the derived class that it is a friend of. Mike Khaw -- internet: mkhaw@teknowledge.com uucp: {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.com hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303