mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (02/10/89)
In the following short C++ program, // test use of "protected" members class bottom { protected: int botint; public: int getbot() { return botint; } }; class higher : public bottom { protected: float hifloat; public: float gethi() { return hifloat; } friend void mumble(higher&); }; main() { higher bar; mumble(bar); } void mumble(higher& foo) { foo.hifloat = 2.718; // this is OK foo.botint = 3; // should this be allowed? return; } Oasys C++ rel. 1.2 complains: ... error: botint is protected Is the complaint valid? If so, why? 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
shopiro@alice.UUCP (Jonathan Shopiro) (02/10/89)
Mike Khaw's example program should be acceptable. The basic rule is, a friend of a class can access anything that the class members can. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229
jenings@hpfclp.SDE.HP.COM (Byron T. Jenings Jr.) (02/11/89)
This compiles fine for me. I'm using the AT&T cfront.
mwg@inxsvcs.UUCP (Phil Blecker) (02/12/89)
In article <26619@teknowledge-vaxc.ARPA>, mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) questsion: > // test use of "protected" members > > ... error: botint is protected > Is the complaint valid? If so, why? I don't know what the official definition of protected is, but I know how it seems to work. Protected means that only derived classes have access to the members. That doesn't include friends. Friends only have access to the private and public members declared in the class the made them friends. Being a friend doesn't give access to base classes, even when protected is used in the base class. You have to do that explicitly. Makes sense, and it seems to be the way it works. -- Phil Blecker +1 818 243-3053 none of my ideas belong to me and uunet!inxsvcs!mwg i can't see anything wrong with that
shankar@hpclscu.HP.COM (Shankar Unni) (02/14/89)
> class higher : public bottom ^^^^^^ > { > protected: > float hifloat; > > public: > float gethi() { return hifloat; } > > friend void mumble(higher&); > }; The 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 { ... } ---- Shankar
mkhaw@teknowledge-vaxc.ARPA (Mike Khaw) (02/16/89)
<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