Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: A TC++PL pr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 9539 of 9830
Post > Topic >>

Re: A TC++PL protected class question

by "Bo Persson" <bop@[EMAIL PROTECTED] > Apr 27, 2008 at 07:12 AM

stevendaiy wrote:
> While preparing for a c++ interview, I went through TC++PL(3rd
> edition, 24th) and studied Appendix C, i.e., technicalities.
> Specifically, I found one of the examples is puzzling to me on page
> 851, i.e., when a class Y2 is derived from a protected base X
> class, and another class Z2 is derived from the public base class
> Y2, in the memeber function Z2::f(Y2* py2)
> if we tried to do something like
>
> class X{
> public:
> int a;
> };
> class Y2: protected X{};
> class Z2:public Y2{void f(Y2*);};
>
> void Z2::f(Y2* py2)
> {
> X* px = this; //OK
> px = py2; //error
> }
> according to the book, the second statement is an error
> //error: X is a protected base of Y2, and Z2 is derived from Y2,
> //but we don't know if py2 is a Z2 or how Y2::X is used in a non-Z2
> object
>
> My first question is that: isn't Y2 a member of Z2 (aka base
> class), so that access to X from Y2 is solely based on the
> relations between Y2 and X.
> I think it's equivalent to
> void Y2::g(Y2 *py2)  //notice Y2:: not Z2::
> {
>   X* px = py2; //OK
> }
>

I have an old 5th printing of the book, where this example is indeed
supposed to work.

There has been changes to the access rules, like this one:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2563.html#9



Perhaps we should consider this a corner case, that should be avoided?
:-)


Bo Persson




-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 4 Posts in Topic:
A TC++PL protected class question
"stevendaiy" &l  2008-04-24 01:03:00 
Re: A TC++PL protected class question
"stevendaiy" &l  2008-04-26 16:26:34 
Re: A TC++PL protected class question
"Bo Persson" &l  2008-04-27 07:12:20 
Re: A TC++PL protected class question
"stevendaiy" &l  2008-04-28 16:06:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:39:20 CDT 2008.