Topic: Q: protected access
Author: James.Kanze@dresdner-bank.com
Date: 1999/06/16 Raw View
In article <7k5q6d$gck$1@panix.com>,
comeau@comeaucomputing.com wrote:
> If you have compilers which allow functions named 'do', they you
really
> have a problem (I'll assume it was just for your example). Re the
issue
> you raise: accessibility is not for all objects, but for this's
object.
> a != this, even though there is a this->a (it's a different A
object!!).
Accessibility is for all objects, not just this. But in the case of
protected, it is only for objects with a static type of the derived
class; if x is protected, it means that a Derived can access the x in
Base in all Derived objects, but not in Base objects whose (static) type
is not Derived.
--
James Kanze mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orient e objet/
Beratung in objekt orientierter
Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86
27
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: "Paul A. Houghton" <phough@rmi.net>
Date: 1999/06/15 Raw View
Could someone answer a question I have about protected access? I was
porting some code
to the sun 5.0 c++ compiler that is supposed to be standard, and was
wondering if the compiler is 'strictly' compilant or not.
Here's the issue:
class A
{
protected:
void dothis( void );
};
class B : public class A
{
private:
void do( void ) {
dothis(); // ok -> protected access
a->dothis() ; // sun compiler says NO, (other compilers say
it's ok
};
A * a;
}
So, the question is should 'B'be able to call a->dothis() since it's a
subclass of 'A'?
thanks,
paul.
--
/\ __@
/ \/\ _-\<,_ IMHO - paul.houghton@wcom.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: wmm@fastdial.net
Date: 1999/06/16 Raw View
In article <37641915.84B15D15@rmi.net>,
paul.houghton@wcom.com wrote:
> Here's the issue:
>
> class A
> {
> protected:
> void dothis( void );
> };
>
> class B : public class A
> {
> private:
> void do( void ) {
> dothis(); // ok -> protected access
> a->dothis() ; // sun compiler says NO, (other compilers say
> it's ok
> };
> A * a;
> }
>
> So, the question is should 'B'be able to call a->dothis() since it's
a
> subclass of 'A'?
The Sun compiler is correct. According to 11.5p1,
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 clause 11. Except when forming a pointer to
member (5.3.1), the access must be through a pointer to,
reference to, or object of the derived class itself (or
any class derived from that class)(5.2.5).
The rationale for the restriction can be found in the ARM on pp 254f.
Basically, different derived classes can use protected members in
ways that reflect incompatible assumptions; allowing derived class
D1 to access the protected base members of derived class D2
(presumably through a conversion to Base*) is an invitation to
disaster.
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: comeau@panix.com (Greg Comeau)
Date: 1999/06/16 Raw View
In article <37641915.84B15D15@rmi.net> paul.houghton@wcom.com writes:
> Could someone answer a question I have about protected access? I was
>porting some code
> to the sun 5.0 c++ compiler that is supposed to be standard, and was
>wondering if the compiler is 'strictly' compilant or not.
>
> Here's the issue:
>
> class A
> {
> protected:
> void dothis( void );
> };
>
> class B : public class A
> {
> private:
> void do( void ) {
> dothis(); // ok -> protected access
> a->dothis() ; // sun compiler says NO, (other compilers say
>it's ok
> };
> A * a;
> }
>
> So, the question is should 'B'be able to call a->dothis() since it's a
>subclass of 'A'?
If you have compilers which allow functions named 'do', they you really
have a problem (I'll assume it was just for your example). Re the issue
you raise: accessibility is not for all objects, but for this's object.
a != this, even though there is a this->a (it's a different A object!!).
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.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: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/06/16 Raw View
On 15 Jun 99 06:32:06 GMT, Paul A. Houghton <phough@rmi.net> wrote:
> class A
> {
> protected:
> void dothis( void );
> };
>
> class B : public class A
> {
> private:
> void do( void ) {
> dothis(); // ok -> protected access
> a->dothis() ; // sun compiler says NO, (other compilers say
>it's ok
> };
> A * a;
> }
The rule is that class B can only access the protected members of another
B object. This rule makes encapsulation stronger. So the SUN compiler
is right in allowing the first line and rejecting the second. Consider
this too
A * me=this; // cast from B* to A*
this->dothis(); // ok
me->dothis(); // error (yet it is pretty much the same as the above)
--
----------------------------------
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 ]