Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11669
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Protected member inaccessible from derived class - why |
| Date | 2011-11-21 20:36 +0000 |
| Organization | Netfront http://www.netfront.net/ |
| Message-ID | <jaecot$2igm$1@adenine.netfront.net> (permalink) |
| References | <90fac08a-2e86-4621-9b08-68e9d2f4d288@w7g2000yqc.googlegroups.com> <jadhhd$vo1$1@dont-email.me> <LYudnaCQn4GFBlfTnZ2dnUVZ8nudnZ2d@giganews.com> |
"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:LYudnaCQn4GFBlfTnZ2dnUVZ8nudnZ2d@giganews.com...
> On 21/11/2011 12:51, Victor Bazarov wrote:
>> On 11/21/2011 6:10 AM, avasilev wrote:
>>> Quick question: why doesn't this code compile:
>>>
>>> class Base
>>> {
>>> protected:
>>> int a;
>>> };
>>>
>>> class Derived: public Base
>>> {
>>> public:
>>> void test(Base* b)
>>> {
>>> b->a = 5;
>>> }
>>> };
>>>
>>> int main()
>>> {
>>> Derived d;
>>> Derived d1;
>>> d.test(&d1);
>>> }
>>> The error is that Base::a is protected in the test() method of Derived
>>
>> The access to protected members is allowed only for the '*this' object,
>
> Wrong.
>
> class Base
> {
> protected:
> int a;
> };
>
> class Derived: public Base
> {
> public:
> void test(Derived* d)
> {
> d->a = 5;
> }
> };
>
> int main()
> {
> Derived d;
> Derived d1;
> d.test(&d1);
> }
>
lol
Well done Leigh! :)
You are gaining experience young Jedi.
--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Protected member inaccessible from derived class - why avasilev <alxvasilev@gmail.com> - 2011-11-21 03:10 -0800
Re: Protected member inaccessible from derived class - why Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-21 07:51 -0500
Re: Protected member inaccessible from derived class - why avasilev <alxvasilev@gmail.com> - 2011-11-21 05:10 -0800
Re: Protected member inaccessible from derived class - why "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-21 16:41 +0100
Re: Protected member inaccessible from derived class - why Leigh Johnston <leigh@i42.co.uk> - 2011-11-21 18:40 +0000
Re: Protected member inaccessible from derived class - why "Paul" <pchrist<nospam>or@yahoo.co.uk> - 2011-11-21 20:36 +0000
csiph-web