Topic: protected members and inheritance
Author: horst.kraemer@snafu.de (Horst Kraemer)
Date: 1998/09/07 Raw View
On 4 Sep 1998 16:24:28 GMT, Ryszard Kabatek
<rysio@rumcajs.chemie.uni-halle.de> wrote:
>The sample below does not compile,
>because 'x' is a protected member of 'A'.
>But 'B' does inherit 'x' from 'A'!
>Why is this not allowed?
>class A {
> public:
> virtual ~A() {}
> protected:
> int x;
>};
>class B : public A {
> public:
> int func1(A& a) {return a.x;} // Does not compile!
> int func2(B& b) {return b.x + b.y;} // OK
> protected:
> int y;
>};
If class B inherits a protected member of class A, class B may only
access this member through an object of class B, i.e. 'x' is only
available to B as a member of its _own_ base class A.
Otherwise anybody could make public the protected member x of the
"pure" class A just by writing a function int& B::func1(A&) in a class
B:A. If somebody else would define now a class C:A without defining a
public member function permitting public access to C's 'x' member, you
would be able to access _his_ C::x through
class B:public A {
public:
int& B::func1(A& a) { return a.x; } // ERROR
};
C c; // somebody else's class
B b;
B.func1(c) = 42; // c.x=42 !!!
just as if x was a public member of C.
Regards
Horst
---
[ 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: "Nate Lewis" <nlewis@mindspring.com>
Date: 1998/09/04 Raw View
>The sample below does not compile,
>because 'x' is a protected member of 'A'.
>
>But 'B' does inherit 'x' from 'A'!
>Why is this not allowed?
>
>class A {
> protected:
> int x;
>};
>
>class B : public A {
> public:
> int func1(A& a) {return a.x;} // Does not compile!
>};
Because:
11.5 Protected member access [class.protected]
1 When a friend or a member function of a derived class references a
protected nonstatic member of a base class, an access check applies in
addition to those described earlier in this clause.4) Except when
forming a pointer to member (_expr.unary.op_), the access must be
through a pointer to, reference to, or object of the derived class
itself (or any class derived from that class) (_expr.ref_).
--
Nate Lewis, MCSD
nlewis@mindspring.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: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
Date: 1998/09/04 Raw View
Hi!
The sample below does not compile,
because 'x' is a protected member of 'A'.
But 'B' does inherit 'x' from 'A'!
Why is this not allowed?
class A {
public:
virtual ~A() {}
protected:
int x;
};
class B : public A {
public:
int func1(A& a) {return a.x;} // Does not compile!
int func2(B& b) {return b.x + b.y;} // OK
protected:
int y;
};
Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129
[ 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: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/09/04 Raw View
On 4 Sep 1998 16:24:28 GMT, Ryszard Kabatek
>The sample below does not compile,
>because 'x' is a protected member of 'A'.
>
>But 'B' does inherit 'x' from 'A'!
>Why is this not allowed?
The A part of B is a completely different class from A.
It's sort of like this:
class A { protected: int x; }
class B { protected: int x; protected: int y; }
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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 ]