Topic: Protection and Overloaded Functions


Author: Tony Edwards <tledwar@sandia.gov>
Date: 1996/11/14
Raw View
I seem to have stumbled upon something I think one should not be able
to do and need to know why it is allowed.

A had a base class function defined as protected and
overloaded the function in the derived class, but accidently created
it as public.

I would think that one should not be able to change the access rules
on a function. I keep seeing references to not increasing the access
to a member or member function in the ARM.

Any answers?
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Chelly Green <chelly@eden.com>
Date: 1996/11/14
Raw View
Tony Edwards wrote:
>
> I seem to have stumbled upon something I think one should not be able
> to do and need to know why it is allowed.
>
> A had a base class function defined as protected and
> overloaded the function in the derived class, but accidently created
> it as public.
>
> I would think that one should not be able to change the access rules
> on a function.

You didn't change the access for the protected function. You created a
new function and gave it public access. What if the base class were
private, i.e. an implementation detail? You wouldn't want to restrict
public names of the class to those that didn't match those of the
private base class. Same goes for protected.

The trouble comes when you override virtual functions. It is confusing
if you put an override in a different access level because the function
may be accessible in a base class, but not in a publically-derived class
(or vice versa). This is why I dislike private virtual functions, since
they can be overridden in a derived class.

> I keep seeing references to not increasing the access
> to a member or member function in the ARM.

Yes, with an access declaration. You can only give the function (from
the base class, no new functions defined in the derived class) the same
visibility in a derived class as it had in the base class (where the
function was made less visible by protected or private derivation). Of
course, by writing a forwarding function, you can effectively give the
base class function more visibility than it had in the base class.

> Any answers?

--
Chelly Green | chelly@eden.com | C++ - http://www.eden.com/~chelly
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]