Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!
From: David F (David-White_at_earthlink.net)
Date: 02/14/04
- Next message: Andre Vachon [MS]: "Re: evaluating expression?"
- Previous message: Carl Daniel [VC++ MVP]: "Re: gcc question"
- In reply to: Igor Tandetnik: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: John Carson: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: John Carson: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: tom_usenet: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: Andre Vachon [MS]: "Re: evaluating expression?"
- Previous message: Carl Daniel [VC++ MVP]: "Re: gcc question"
- In reply to: Igor Tandetnik: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: John Carson: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: John Carson: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: tom_usenet: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|