Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: David F (David-White_at_earthlink.net)
Date: 02/14/04


Date: Sat, 14 Feb 2004 02:59:14 GMT

Thanks Igor.

So if that is the case, it means that the only "clean" solution is to go all
the time through a public function of the base or contained class, since the
other two options I mentioned are either undesirable (making the data
members public) or unacceptable (declaring friends, and this is because when
this class is designed, it does not know about the derived or containing
classes that will come later). On the other hand, in many complicated cases
and/or very large dynamic data containers, going through the function call
may be prohibitively costly in run time.

And if so, I think it was a mistake by the language designer not to
compromise by making a better use of protected members of base or contained
class by giving the protected members special direct access rights through
pointers (limited only by properly qualified by type).

By the way, what is exactly the "C++ Standard" you are referring to? Is it
available on the web (especially a soft copy of it)?
The only reference I have is a book called "The Annotated C++ Reference
Manual" by Ellis & Stroustrup but it is old (October 1992 reprint of the
1990 original).

Thanks,
David

"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OMSlOwn8DHA.2572@TK2MSFTNGP09.phx.gbl...
> "David F" <David-White@earthlink.net> wrote in message
> news:a2bXb.2877$WW3.965@newsread2.news.pas.earthlink.net...
> > Y::f( ) CAN access the base class' protected member (X::xpt in the
> example)
> > directly but can't access the very same member through a pointer (t)!
> unless
> > one of the two alterations suggested in the comments is made. No where
> did I
> > read in the "C++ Programming Language" book about such a limitation.
> >
> > Here is the example program:
> > -----------------------------
> > #include <iostream>
> > class X {
> > // friend class Y; // see the error note below
> > protected:
> > int xpt;
> > };
> > class Y : public X {
> > public:
> > void f();
> > };
> > void Y::f() {
> > xpt=1; // OK !
> > X* t=new X;
> > t->xpt=2; // error C2248: 'X::xpt' : cannot access
> protected
> > member declared in class 'X'
>
> This is by design. C++ Standard 11.5:
>
> 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).
>
> In other words, you don't have any special access to protected members
> of an arbitrary unrelated object of class X, except when this X happens
> to be a base subobject of some Y object and you access those members
> through pointer or reference to Y.
> --
> With best wishes,
> Igor Tandetnik
>
> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken
>
>



Relevant Pages

  • Re: about reference
    ... >> Hello members, ... >> What's the difference between Perl's reference and C's pointer? ... what about the fact that perl refs can't be cast into other types ...
    (perl.beginners)
  • Re: Some Questions Asked in Interview
    ... >> TO step through the array, or pass it by reference to another function. ... > You can't step through an array using a pointer to it. ... I would either keep a count on members or ...
    (comp.lang.c)
  • Group objects
    ... through the common instance reference. ... If two or more members have duplicate methods either the first ... algorithms and horizontal inheritance. ... Being born as a part of the dynamic language study the group object ...
    (comp.lang.misc)
  • Re: A very good blog about the Delphi 2007 Help Issue
    ... To be also 'critical', I'll dare to mention some points in the _present_ help system, ie. the latest .CHM files: ... is better to present the members also in an 'inherited' way and/or put a symbol to mark that this member is inherited. ... The VCL help is titled 'RAD Studio VCL Win32 Reference M-Z' while it contains in fact the entire reference. ... The Delphi compiler does not provide RTTI by default. ...
    (borland.public.delphi.non-technical)
  • Re: [Beginner] question on Class Diagram
    ... Flight: holds flight information ... The relationship simply defines the overall structure of how individual members of each class set may be logically related. ... in UML relationships are subdivided for specialized purposes like generalization and composition. ... There are three ways to implement a relationship: invoking a FIND search on the class set; passing an object reference as a method argument; and embedding a reference to another object in an object. ...
    (comp.object)