Topic: can not access protected members via pointer...why?


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 11 Jul 01 19:05:24 GMT
Raw View
On 9 Jul 2001 12:55:19 -0400, "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
wrote:

> "Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
> news:mjPs0wC8TQR7Ew60@ntlworld.com...

> > In article <9hugqm$s6l$1@sunnews.cern.ch>, Maciej Sobczak
> > <Maciej.Sobczak@cern.ch> writes

> > >Yes, indeed. The Standard (5.4-7) says that static_cast and
> reinterpret_cast
> > >should work even when the base class is  not accessible,
> >
> > Where? I think you will find that you may not use static_cast to an
> > inaccessible base.
>
> ???
> I don't claim I understand the whole Standard (otherwise I would set up a
> cunsulting company), but the fragment pointed by me (5.4-7) seems to be
> clear, IMHO.
> Of course, the fact that VC++ and g++ get it different means there is a
> place for discussion. Could you please elaborate?

Subtle reading error.  A static_cast to base requires the base to be
accessible.  Using the C-style cast notation permits it anyway.  It
is not what you say, but how you say it.

struct B1 { int b; };
struct B2 { int b; };
struct D : public B1, private B2 { };
void f () {
   D d;
   B2* p1(&d);    // invalid, no standard conversion 4.10/3
   B2* p2(static_cast<B2*>(&d));   // still invalid 5.2.9
   B2* p3((B2*)&d);                // valid 5.4/7
   B2* p4(reinterpret_cast<B2*>(&d));  // valid 5.2.10/7
                                       // but what do you get?
   }

The question on the last one is in case of multiple inheritance.  You
will likely get a bad pointer for one of the two bases regardless of
whether they are private or not.  The C-style will (hopefully) do the
static_cast form and give the correct pointer for both.

John

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: 12 Jul 2001 14:18:09 -0400
Raw View
Hi,

"John Potter" <jpotter@falcon.lhup.edu> wrote in message
news:3b4be962.12463754@news.csrlink.net...
> On 9 Jul 2001 12:55:19 -0400, "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
> wrote:
>
> > > >Yes, indeed. The Standard (5.4-7) says that static_cast and
> > reinterpret_cast
> > > >should work even when the base class is  not accessible,
> > >
> > > Where? I think you will find that you may not use static_cast to an
> > > inaccessible base.
> >
> > ???
> > I don't claim I understand the whole Standard (otherwise I would set up
a
> > cunsulting company), but the fragment pointed by me (5.4-7) seems to be
> > clear, IMHO.
> > Of course, the fact that VC++ and g++ get it different means there is a
> > place for discussion. Could you please elaborate?
>
> Subtle reading error.

Yes. After reading your examples I get it now. 5.4-7 is about exlicit type
conversions, not what you can do with static_cast and reinterpret_cast.

>  A static_cast to base requires the base to be
> accessible.  Using the C-style cast notation permits it anyway.  It
> is not what you say, but how you say it.
>
> struct B1 { int b; };
> struct B2 { int b; };
> struct D : public B1, private B2 { };
> void f () {
>    D d;
>    B2* p1(&d);    // invalid, no standard conversion 4.10/3


g++ swallows this line happily - this was the source of my confusion, I
think:
>    B2* p2(static_cast<B2*>(&d));   // still invalid 5.2.9

MSVC++7.0 beta produces a warning for this line:
>    B2* p3((B2*)&d);                // valid 5.4/7

>    B2* p4(reinterpret_cast<B2*>(&d));  // valid 5.2.10/7
>                                        // but what do you get?
>    }

Thank you for clarification.


Interested in distributed, object-based programming?
http://www.cern.ch/maciej/prog/yami

Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: 06 Jul 01 18:34:23 GMT
Raw View
In article <9hugqm$s6l$1@sunnews.cern.ch>, Maciej Sobczak
<Maciej.Sobczak@cern.ch> writes
>Yes, indeed. The Standard (5.4-7) says that static_cast and reinterpret_cast
>should work even when the base class is  not accessible,

Where? I think you will find that you may not use static_cast to an
inaccessible base.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: "Andrew Wall" <ajwall@nospam.geocities.com>
Date: Fri, 6 Jul 2001 18:48:52 GMT
Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:qzHvw+CdcQR7Ewa6@ntlworld.com...
> In article <SV417.5984$Jk6.1326002@news2-win.server.ntlworld.com>,
> Andrew Wall <ajwall@nospam.geocities.com> writes
> >void Z2::f(Y1*py1,Y2*py2,Y3*py3){
> >a=7;                // Obvious, my own 'a', even though it is protected
> >X*px=py1;        // Anyone can perform this conversion
> >px->a=7;
> >py1->a=7;        // Everyone has this access
> >
> >/*
> >px=py2;            // I had to comment these out to get the program to
> >compile
> >py2->a=7;
> >
> >px=py3;
> >py3->a=7;
> >*/
> >
> >}
> >
> >And yet Stroustrup says: (about px=py2;)  ok: X is a protected base of
Y2,
> >and Z2 is derived from Y2
>
> I think you have over-simplified.
Not quite with you there, I'm just quoting Stroustrup.

>Remember that a derived class only has
> access to the protected part of itself, not to through any old instance
> of the base.
>
Yes, the 'a=7' above shows I can access my own 'a'.

> Now py2 is just a pointer to Y2, but not to part of Z so in the context
> of Z there is no access to X other than through 'this' The conversions
> you are asking for are, I think, highly dangerous and I think a careful
> analysis will show the compiler is right (but I am not making a bet
> because this area is fraught with traps)
Yes, they probably _are_ highly dangerous, but my question still stands:
Why does Stroustrup give as an example of the differences between public,
protected and private inheritance some code that doesn't compile due to
access violations??


--
Andrew

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Sat, 7 Jul 2001 23:04:29 GMT
Raw View
In article <GXl17.1127$WS4.189367@news6-win.server.ntlworld.com>, Andrew
Wall <ajwall@nospam.geocities.com> writes
>Yes, they probably _are_ highly dangerous, but my question still stands:
>Why does Stroustrup give as an example of the differences between public,
>protected and private inheritance some code that doesn't compile due to
>access violations??

Because, despite claims to the contrary by fans, he isn't God (sorry
Bjarne what you wrote is probably OK, I just cannot be bothered to look,
and it is the attitude that because you wrote it, it MUST be right that
irritates me) nor the Pope.

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: 9 Jul 2001 12:55:19 -0400
Raw View
Hi,

"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:mjPs0wC8TQR7Ew60@ntlworld.com...
> In article <9hugqm$s6l$1@sunnews.cern.ch>, Maciej Sobczak
> <Maciej.Sobczak@cern.ch> writes
> >Yes, indeed. The Standard (5.4-7) says that static_cast and
reinterpret_cast
> >should work even when the base class is  not accessible,
>
> Where? I think you will find that you may not use static_cast to an
> inaccessible base.

???
I don't claim I understand the whole Standard (otherwise I would set up a
cunsulting company), but the fragment pointed by me (5.4-7) seems to be
clear, IMHO.
Of course, the fact that VC++ and g++ get it different means there is a
place for discussion. Could you please elaborate?

For clarification: I understand that I can take a pointer to the derived
class and static_cast or reinterpret_cast it to the pointer to the
unambiguous base class, even if it's normally inaccessible (due to the
access specifiers).

Thank you.


Interested in distributed, object-based programming?
http://www.cern.ch/maciej/prog/yami

Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrew Wall" <ajwall@nospam.geocities.com>
Date: 30 Jun 01 06:07:49 GMT
Raw View
"Maciej Sobczak" <Maciej.Sobczak@cern.ch> wrote in message
news:9gcf64$p60$1@sunnews.cern.ch...
> Hi,
> "Peter Quiring" <pquiring@geocities.com> wrote in message
> news:rkDT6.83754$r7.11239572@news1.busy1.on.home.com...
> > Why are the two last lines not valid?  They should be by my reasoning.
<snip>
</snip>
> Standard says (11.5-1):
>
> "When a [...] member function of a derived class references a protected
> nonstatic member of a base
> class, [...] the access must be through a pointer to, reference to, or
> object of the derived
> class itself"
>
> You're trying to access the protected members through the pointer to the
> *base* class, that's why compiler complains.
> (and that's why in the constructor of C1 it works)
>

I thought that this should be allowed after reading C++PL 3e (Stroustrup)!

C.11.2 Access to Base Classes

This has an example which I typed into VC++ 6

class X{
public:
 int a;
};
class Y1:public X{};
class Y2:protected X{};
class Y3:private X{};

class Z2:public Y2{
void f(Y1*,Y2*,Y3*);
};

void Z2::f(Y1*py1,Y2*py2,Y3*py3){
a=7;
X*px=py1;
px->a=7;
py1->a=7;

/*
px=py2;            // I had to comment these out to get the program to
compile
py2->a=7;

px=py3;
py3->a=7;
*/

}

And yet Stroustrup says:  ok: X is a protected base of Y2, and Z2 is derived
from Y2

Why is this?
What am I missing?


--
Andrew


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: 2 Jul 2001 12:32:56 -0400
Raw View
Hi,
"Andrew Wall" <ajwall@nospam.geocities.com> wrote in message
news:4O0%6.40223$mK4.3970835@news6-win.server.ntlworld.com...

> > "When a [...] member function of a derived class references a protected
> > nonstatic member of a base
> > class, [...] the access must be through a pointer to, reference to, or
> > object of the derived
> > class itself"
> >
> > You're trying to access the protected members through the pointer to the
> > *base* class, that's why compiler complains.
> > (and that's why in the constructor of C1 it works)
> >
>
> I thought that this should be allowed after reading C++PL 3e (Stroustrup)!
>
> C.11.2 Access to Base Classes
>
> This has an example which I typed into VC++ 6
>
> class X{
> public:
>  int a;
> };
> class Y1:public X{};
> class Y2:protected X{};
> class Y3:private X{};
>
> class Z2:public Y2{
> void f(Y1*,Y2*,Y3*);
> };
>
> void Z2::f(Y1*py1,Y2*py2,Y3*py3){
> a=7;
> X*px=py1;
> px->a=7;
> py1->a=7;
>
> /*
> px=py2;            // I had to comment these out to get the program to
> compile
> py2->a=7;
>
> px=py3;
> py3->a=7;
> */
>
> }
>
> And yet Stroustrup says:  ok: X is a protected base of Y2, and Z2 is
derived
> from Y2
>
> Why is this?
> What am I missing?

You had to comment out the fragment above, because (Standard: 4.10-3 and
11.2-4) the is *no* implicit conversion from the pointer to the derived to
the pointer to the base if the base is not accessible (and the base is not
acessible if its invented public member is not accessible (which happens
with protected and private inheritance)).
You can make it work with explicit conversions (casts).

You can ask: why "a = 7;" works? It works thanks to the rule cited at the
very top of this message.
Thanks to the same rule, "py2->a = 7;" and "py3->a = 7;" will not work.

Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"

Interested in distributed, object-based programming?
http://www.cern.ch/maciej/prog/yami
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrew Wall" <ajwall@nospam.geocities.com>
Date: 03 Jul 01 17:28:51 GMT
Raw View
Thanks for the pointer, Maciej.
Could you perhaps also explain though, why C++PL 3e seems to imply that it
_is_ permissible?

"Maciej Sobczak" <Maciej.Sobczak@cern.ch> wrote in message
news:9hpa2l$48f$1@sunnews.cern.ch...
> Hi,
> "Andrew Wall" <ajwall@nospam.geocities.com> wrote in message
> news:4O0%6.40223$mK4.3970835@news6-win.server.ntlworld.com...
>
<snip>
</snip>
> > /*
> > px=py2;            // I had to comment these out to get the program to
> > compile
> > py2->a=7;
> >
> > px=py3;
> > py3->a=7;
> > */
> >
> > }
> >
> > And yet Stroustrup says:  ok: X is a protected base of Y2, and Z2 is
> derived
> > from Y2
> >
> > Why is this?
> > What am I missing?
>
> You had to comment out the fragment above, because (Standard: 4.10-3 and
> 11.2-4) the is *no* implicit conversion from the pointer to the derived to
> the pointer to the base if the base is not accessible (and the base is not
> acessible if its invented public member is not accessible (which happens
> with protected and private inheritance)).
> You can make it work with explicit conversions (casts).
I've just tried this with VC++:
px=(X*)py2;

but this doesn't work either (conversion is inaccessible)
>
> You can ask: why "a = 7;" works? It works thanks to the rule cited at the
> very top of this message.
> Thanks to the same rule, "py2->a = 7;" and "py3->a = 7;" will not work.
>
I can see this:  "a=7"  is refering to my own "a", and because it is
protected inheritance I can still access it.

The reason I was interested in it was that I wanted instances of the same
class to be able to access protected members of each other, but would also
mean that all other class would not have the same access.

I can solve this problem like this:

class X{
public:
 int a;
};
class Y1:public X{};
class Y3;
class Y2:protected X{
void g(Y1*py1,Y2*py2,Y3*py3);
protected:
 X* conv(Y2*py2){return py2;}
};
class Y3:private X{};

class Z2:public Y2{
void f(Y1*,Y2*,Y3*);
};

void Z2::f(Y1*py1,Y2*py2,Y3*py3){
a=7;
X*px=py1;
px->a=7;
py1->a=7;

py2=this;
px=conv(py2);        // Even though I (Z2) cannot perform the conversion, my
base (Y2) can do it!
px->a=7;
/*
px=py3;
py3->a=7;
*/

}


Andrew



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: 5 Jul 2001 16:42:31 -0400
Raw View
Hi,
"Andrew Wall" <ajwall@nospam.geocities.com> wrote in message
news:vy407.6394$Ms3.1156091@news6-win.server.ntlworld.com...
> Thanks for the pointer, Maciej.
> Could you perhaps also explain though, why C++PL 3e seems to imply that it
> _is_ permissible?

No, because this text is not in my cache anymore and I don't have it at
hand...

> > You can make it work with explicit conversions (casts).
> I've just tried this with VC++:
> px=(X*)py2;
>
> but this doesn't work either (conversion is inaccessible)

Yes, indeed. The Standard (5.4-7) says that static_cast and reinterpret_cast
should work even when the base class is  not accessible, so the behaviour
you (and me) see seems to be strange. It does not work on VC++6.0 and
VC++7.0 beta, but works fine on g++. For me, g++ is right here.

> The reason I was interested in it was that I wanted instances of the same
> class to be able to access protected members of each other, but would also
> mean that all other class would not have the same access.

This is how it is supposed to work, I think.

> I can solve this problem like this:
>
> class X{
> public:
>  int a;
> };
> class Y1:public X{};
> class Y3;
> class Y2:protected X{
> void g(Y1*py1,Y2*py2,Y3*py3);
> protected:
>  X* conv(Y2*py2){return py2;}
> };
> class Y3:private X{};
>
> class Z2:public Y2{
> void f(Y1*,Y2*,Y3*);
> };
>
> void Z2::f(Y1*py1,Y2*py2,Y3*py3){
> a=7;
> X*px=py1;
> px->a=7;
> py1->a=7;
>
> py2=this;

And what happens to the original value of py2? You throw it away, so the
rest will not work.

> px=conv(py2);

It seems OK in terms of the Standard. This is simpler example:

class X {};
class Y : protected X
{
protected:
    X* conv(Y *py) { return py; }
};
class Z : public Y
{
    void f(Y *py)
    {
         X* px;
        // this shuld not work:
        // px  =py;

        // but this works fine
        px = conv(py);
    }
};

This is fine, although looks strange.


Interested in distributed, object-based programming?
http://www.cern.ch/maciej/prog/yami

Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrew Wall" <ajwall@nospam.geocities.com>
Date: Thu, 5 Jul 2001 17:59:24 CST
Raw View
"Maciej Sobczak" <Maciej.Sobczak@cern.ch> wrote in message
news:9hugqm$s6l$1@sunnews.cern.ch...
> Hi,
> "Andrew Wall" <ajwall@nospam.geocities.com> wrote in message
> news:vy407.6394$Ms3.1156091@news6-win.server.ntlworld.com...
> > Thanks for the pointer, Maciej.
> > Could you perhaps also explain though, why C++PL 3e seems to imply that
it
> > _is_ permissible?
>
> No, because this text is not in my cache anymore and I don't have it at
> hand...
>
So, may I reiterate:

I thought that this should be allowed after reading C++PL 3e (Stroustrup)!

C.11.2 Access to Base Classes  --  ( I typed the following example into VC++
6)

class X{
public:
 int a;
};
class Y1:public X{};
class Y2:protected X{};
class Y3:private X{};

class Z2:public Y2{
void f(Y1*,Y2*,Y3*);
};

void Z2::f(Y1*py1,Y2*py2,Y3*py3){
a=7;                // Obvious, my own 'a', even though it is protected
X*px=py1;        // Anyone can perform this conversion
px->a=7;
py1->a=7;        // Everyone has this access

/*
px=py2;            // I had to comment these out to get the program to
compile
py2->a=7;

px=py3;
py3->a=7;
*/

}

And yet Stroustrup says: (about px=py2;)  ok: X is a protected base of Y2,
and Z2 is derived from Y2

>
> Yes, indeed. The Standard (5.4-7) says that static_cast and
reinterpret_cast
> should work even when the base class is  not accessible, so the behaviour
> you (and me) see seems to be strange. It does not work on VC++6.0 and
> VC++7.0 beta, but works fine on g++. For me, g++ is right here.
I'll try this with Borland when I can.

>
> > The reason I was interested in it was that I wanted instances of the
same
> > class to be able to access protected members of each other, but would
also
> > mean that all other class would not have the same access.
>
> This is how it is supposed to work, I think.
>
> > I can solve this problem like this:
> >
> > class X{
<snip>
</snip>
> >
> > py2=this;
>
> And what happens to the original value of py2? You throw it away, so the
> rest will not work.
Sorry, the above line was an experiment, please ignore.

>
> > px=conv(py2);
>
> It seems OK in terms of the Standard. This is simpler example:
>
> class X {};
> class Y : protected X
> {
> protected:
>     X* conv(Y *py) { return py; }
> };
> class Z : public Y
> {
>     void f(Y *py)
>     {
>          X* px;
>         // this shuld not work:
>         // px  =py;
>
>         // but this works fine
>         px = conv(py);
>     }
> };
>
> This is fine, although looks strange.
>
It looks identical to my (Stroustrup's) example.
So, why should Y be able to convert a pointer to another Y to a pointer to
its X when Z cannot?
Both Y and Z can convert their own 'this' to X*.

Thanks for your feedback.


--
Andrew

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Fri, 6 Jul 2001 01:17:13 GMT
Raw View
In article <SV417.5984$Jk6.1326002@news2-win.server.ntlworld.com>,
Andrew Wall <ajwall@nospam.geocities.com> writes
>void Z2::f(Y1*py1,Y2*py2,Y3*py3){
>a=7;                // Obvious, my own 'a', even though it is protected
>X*px=py1;        // Anyone can perform this conversion
>px->a=7;
>py1->a=7;        // Everyone has this access
>
>/*
>px=py2;            // I had to comment these out to get the program to
>compile
>py2->a=7;
>
>px=py3;
>py3->a=7;
>*/
>
>}
>
>And yet Stroustrup says: (about px=py2;)  ok: X is a protected base of Y2,
>and Z2 is derived from Y2

I think you have over-simplified. Remember that a derived class only has
access to the protected part of itself, not to through any old instance
of the base.

Now py2 is just a pointer to Y2, but not to part of Z so in the context
of Z there is no access to X other than through 'this' The conversions
you are asking for are, I think, highly dangerous and I think a careful
analysis will show the compiler is right (but I am not making a bet
because this area is fraught with traps)

Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: James Dennett <jdennett@acm.org>
Date: 16 Jun 2001 08:55:20 -0400
Raw View
Peter Quiring wrote:
>
> Why are the two last lines not valid?  They should be by my reasoning.
>
> class C1 {
>   protected:
>     int data;
>     C1 *c1c1;
>   public:
>     C1() {
>       c1c1->data=0;  // OK!
>     }
> };
>
> class C2 : public C1 {
>   private:
>     C1 *c2c1;
>   public:
>     C2() {
>       data=1;             // OK!
>       c1c1->data=2;  // NOT VALID!  WHY??
>       c2c1->data=3;  // NOT VALID!  WHY??
>     }
> };

Because C2 does not have access to protected members of C1
objects except as part of a C2 object.  You cannot gain
access to protected parts of classes simply by deriving
from their base class; if you could do so, protected
access would gain nothing over leaving members public.

-- James Dennett
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: pravinkumar_shivakumar@satyam.com (Pravin)
Date: 16 Jun 2001 08:55:39 -0400
Raw View
Hi all,

Let me quote the reference in ARM which is a solution for the above
problem.

Protected member Access
-----------------------

A friend or a member function of a derived class can access a
protected static member of a base class.A friend or a member function
of a derived class can access a protected non static member of one of
its base classes only through a pointer to,reference to or object of
the derived class(or any class derived from that class)..............

Consider allowing a derived class to access protected members of any
object with the type of one of its base classes.That is relax the
restriction that access to a protected member must be through a
pointer to, reference to ,or object of the derived class.This would
allow a class to access the base class part of an unrelated class
without the use of an explicit cast.This would be the only place where
the language allowed that.

Pravin kumar . S
Software Engineer, Microsoft Competency Group.
Satyam Computer Services Limited,
A North Block, Tidel Park,
Chennai.India.
Ph  : 2540320 - 6664
Mob : (0) 98410 50508






"Peter Quiring" <pquiring@geocities.com> wrote in message news:<rkDT6.83754$r7.11239572@news1.busy1.on.home.com>...
> Why are the two last lines not valid?  They should be by my reasoning.
>
> class C1 {
>   protected:
>     int data;
>     C1 *c1c1;
>   public:
>     C1() {
>       c1c1->data=0;  // OK!
>     }
> };
>
> class C2 : public C1 {
>   private:
>     C1 *c2c1;
>   public:
>     C2() {
>       data=1;             // OK!
>       c1c1->data=2;  // NOT VALID!  WHY??
>       c2c1->data=3;  // NOT VALID!  WHY??
>     }
> };
>
>
>       [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
>       [ about comp.lang.c++.moderated. First time posters: do this! ]
>
> [ 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://www.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Maciej Sobczak" <Maciej.Sobczak@cern.ch>
Date: 16 Jun 2001 08:55:59 -0400
Raw View
Hi,
"Peter Quiring" <pquiring@geocities.com> wrote in message
news:rkDT6.83754$r7.11239572@news1.busy1.on.home.com...
> Why are the two last lines not valid?  They should be by my reasoning.
>
> class C1 {
>   protected:
>     int data;
>     C1 *c1c1;
>   public:
>     C1() {
>       c1c1->data=0;  // OK!
>     }
> };
>
> class C2 : public C1 {
>   private:
>     C1 *c2c1;
>   public:
>     C2() {
>       data=1;             // OK!
>       c1c1->data=2;  // NOT VALID!  WHY??
>       c2c1->data=3;  // NOT VALID!  WHY??
>     }
> };

Standard says (11.5-1):

"When a [...] member function of a derived class references a protected
nonstatic member of a base
class, [...] the access must be through a pointer to, reference to, or
object of the derived
class itself"

You're trying to access the protected members through the pointer to the
*base* class, that's why compiler complains.
(and that's why in the constructor of C1 it works)

Maciej Sobczak, http://www.cern.ch/Maciej.Sobczak
"in theory, there is no difference between theory and practice - but in
practice, there is"
---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Peter Quiring" <pquiring@geocities.com>
Date: 14 Jun 01 16:38:00 GMT
Raw View
Why are the two last lines not valid?  They should be by my reasoning.

class C1 {
  protected:
    int data;
    C1 *c1c1;
  public:
    C1() {
      c1c1->data=0;  // OK!
    }
};

class C2 : public C1 {
  private:
    C1 *c2c1;
  public:
    C2() {
      data=1;             // OK!
      c1c1->data=2;  // NOT VALID!  WHY??
      c2c1->data=3;  // NOT VALID!  WHY??
    }
};


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]