Topic: Question about protected members
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/13 Raw View
MEB <blueming@uni-duesseldorf.de> writes:
>Why is the language designed to forbid the following code:
>class A
>{
>protected:
> int i;
>};
>class B : public A
>{
>public:
> B( A &a)
> {
> i = a.i; // access to protected i in class A not allowed
> }
>};
See the ARM, page 254. It would allow members of B to access
any A object or any A subobject of an unrelated type:
class C : public A {
...
};
void B::foo(C* pc) { pc->A::i = 4; }
The code modifies the protected state of an unrelated object.
That isn't the purpose of protected access. The idea is that
you can access the protected state of an object of your own
type or a derived type, not of other types.
This rule has been part of C++ as long as protected access has
been in the language. It is still in the standard, section
11.5, paragraph 1.
--
Steve Clamage, stephen.clamage@sun.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/05/14 Raw View
MEB wrote:
> Why is the language designed to forbid the following code:
>
> class A
> {
> protected:
> int i;
> };
>
> class B : public A
> {
> public:
> B( A &a)
> {
> i = a.i; // access to protected i in class A not allowed
> }
> };
>
> I really can't understand why it's not allowed to access a.i while
> it's totally allowed to access this->i in class B.
As other posters have explained, you're attempting to access a
protected member of object 'a'.
One solution is to make class B a friend of class A. This has
the drawback of allowing B to access all of A's private members,
though.
Another solution is to cast 'a' (an A&) to a B&, and then access
the protected member from there. This means that 'a' must be a B
or a type derived from B in order to work correctly, though.
Another solution is to add accessor functions to class A that give
B access to the protected member. This approach probably has fewer
drawbacks than the other two.
-- David R. Tribble, dtribble@technologist.com --
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "William M. Miller" <wmm@fastdial.net>
Date: 1999/05/10 Raw View
MEB <blueming@uni-duesseldorf.de> wrote in message
news:7h3s6n$4v4@selena.rz.uni-duesseldorf.de...
> Why is the language designed to forbid the following code:
>
> class A
> {
> protected:
> int i;
> };
>
> class B : public A
> {
> public:
> B( A &a)
> {
> i = a.i; // access to protected i in class A not allowed
> }
> };
>
>
> I get the error with VC++ 5.0.
> Is it a language feature or a compiler bug?
>
> I really can't understand why it's not allowed to access a.i while it's
> totally allowed to access this->i in class B.
It's not a bug; it's part of the language. The ARM has a discussion of
the rationale behind this decision, if you can find a copy. Basically,
a protected member is part of the base class's implementation that
the designer has chosen to make available to derived classes.
There is no guarantee that two different derived classes will use the
protected member in the same way. If a D1 member function is
passed a pointer or reference to a D2 object, assumptions made
about the state of protected members of their common base class
that are valid for D1 objects might be incorrect for D2 objects and
lead to subtle bugs (which apparently actually happened in real-life
code before this restriction was added to the language). To avoid
this problem, member functions are only allowed to access
protected members of a base class if the pointer or reference is
to the member function's class or one derived therefrom.
-- William M. Miller, Software Emancipation Technology, Inc.
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: MEB <blueming@uni-duesseldorf.de>
Date: 1999/05/09 Raw View
Why is the language designed to forbid the following code:
class A
{
protected:
int i;
};
class B : public A
{
public:
B( A &a)
{
i = a.i; // access to protected i in class A not allowed
}
};
I get the error with VC++ 5.0.
Is it a language feature or a compiler bug?
I really can't understand why it's not allowed to access a.i while it's
totally allowed to access this->i in class B.
Mirko Blueming
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]