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


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


Relevant Pages

  • Re: Virtual Data?
    ... > pointer to function. ... type of data for a member variable of the same name, ... But your derived class needs a more complicated variable x, ... virtual Init(const int initX); ...
    (comp.lang.cpp)
  • Re: C2248: cannot access protected member
    ... void foo() ... 11.5/1 When a friend or a member function of a derived class references ... pointer to member, the access must be through a pointer to, ...
    (microsoft.public.vc.language)
  • Re: C2248: cannot access protected member
    ... 11.5/1 When a friend or a member function of a derived class references ... pointer to member, the access must be through a pointer to, ... protected members of its base class X, ...
    (microsoft.public.vc.language)
  • Re: Casting of pointer to member functions
    ... cast it back before any usage. ... The example above will compile only if /ymg compiler switch ... the bits that pointer is ... calling a member function through such pointer is ...
    (microsoft.public.vc.language)
  • Re: Problem bei for Anweisung
    ... wenn man ein char-Feld hat. ... — if it has pointer type, it is initialized to a null pointer; ... if it is an aggregate, every member is initialized ... according to these rules; ...
    (de.comp.lang.c)

Loading