[comp.lang.c++] Why just public, protected, and private?

ravi@xanadu.com (Ravi Pandya) (02/09/91)

For some strange reason, C++ allows private messages to be sent either
to this or to another compatible object, but protected messages can
only be sent to this. I have encountered several cases where I would
like to declare a message that can only be sent from a subclass of the
declaring class, but can be sent to any compatible object. I have also
run into occasions where I would like to declare a method that can
only be sent to this, and only from the declaring class. I would like
to see the protection categories extended to include the following:

===================================================================
||        \  RECEIVER || only to this       | to any object      ||
|| SENDER  \          ||                    | of compatible type ||
===================================================================
|| from declaring     || secret             | private            ||
|| class only         ||                    |                    ||
-------------------------------------------------------------------
|| from any subclass  || protected          | confidential       ||
|| of declaring class ||                    |                    ||
-------------------------------------------------------------------
|| from any class     || equivalent to      | public             ||
||                    || protected          |                    ||
===================================================================

Does anyone know why this particular choice was taken? It would be
more consistent, and in my experience more useful, to put protected
member functions in the right hand column, allowing them to be sent to
any compatible type.
	--ravi

	Ravi Pandya
	Xanadu Operating Company
	550 California Avenue
	Suite 101
	Palo Alto, CA 94306
	415 856 4112 ext 122
	415 856 2251 fax
	ravi@xanadu.com

rfg@NCD.COM (Ron Guilmette) (02/10/91)

In article <1991Feb9.021746.13835@xanadu.com> ravi@xanadu.com (Ravi Pandya) writes:
>For some strange reason, C++ allows private messages to be sent either
>to this or to another compatible object, but protected messages can
>only be sent to this.


I don't have my copy of E&S handy, but I believe that your statement is
incorrect.

As I recall, there are at least as many restrictions on calls to private
member functions as there are on calls to protected member function (if
not more).

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

fuchs@tmipi4.telematik.informatik.uni-karlsruhe.de (Harald Fuchs) (02/10/91)

rfg@NCD.COM (Ron Guilmette) writes:

>In article <1991Feb9.021746.13835@xanadu.com> ravi@xanadu.com (Ravi Pandya) writes:
>>For some strange reason, C++ allows private messages to be sent either
>>to this or to another compatible object, but protected messages can
>>only be sent to this.

>I don't have my copy of E&S handy, but I believe that your statement is
>incorrect.

Me too. I think what Ravi meant was the following: you can call private
member functions of other objects _of the same type_ (because the unit
of protection in C++ is the class rather than the instance), but you can
call protected member functions (in the base type) only for _this_ object,
not for other objects of the base type.
--

Harald Fuchs <fuchs@tmipi4.telematik.informatik.uni-karlsruhe.de>
<fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*

jbuck@galileo.berkeley.edu (Joe Buck) (02/12/91)

In article <fuchs.666199992@tmipi4>, fuchs@tmipi4.telematik.informatik.uni-karlsruhe.de (Harald Fuchs) writes:
|> rfg@NCD.COM (Ron Guilmette) writes:
|> 
|> >In article <1991Feb9.021746.13835@xanadu.com> ravi@xanadu.com (Ravi Pandya) writes:
|> >>For some strange reason, C++ allows private messages to be sent either
|> >>to this or to another compatible object, but protected messages can
|> >>only be sent to this.
|> 
|> >I don't have my copy of E&S handy, but I believe that your statement is
|> >incorrect.
|> 
|> Me too. I think what Ravi meant was the following: you can call private
|> member functions of other objects _of the same type_ (because the unit
|> of protection in C++ is the class rather than the instance), but you can
|> call protected member functions (in the base type) only for _this_ object,
|> not for other objects of the base type.

This is not correct either.  Given classes Base and Derived, and protected
member function Base::func(), a member function of class Derived can call
func() on itself *or* on another object of class Derived.  It may not
call func(), however, on objects of class Base or on objects of other
classes derived from Base and not derived from Derived.

Why, you ask?  So protectedness is not too weak a concept in tree-type
class hierarchies, mainly; as you work with it more the reason become
more clear.

--
Joe Buck
jbuck@galileo.berkeley.edu	 {uunet,ucbvax}!galileo.berkeley.edu!jbuck