Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!
From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 02/13/04
- Next message: a.m.a: "vector::assign ?"
- Previous message: Alex: "Re: 2 classes in the same .h, is it "clean" or against all OOP rules?"
- In reply to: David F: "error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: Naanthaa N: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 13 Feb 2004 16:54:05 -0500
"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: a.m.a: "vector::assign ?"
- Previous message: Alex: "Re: 2 classes in the same .h, is it "clean" or against all OOP rules?"
- In reply to: David F: "error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Next in thread: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: David F: "Re: error C2248: a derived class' f() can't access its base class protected member through a pointer?!"
- Reply: Naanthaa N: "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
|