Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11658 > unrolled thread
| Started by | avasilev <alxvasilev@gmail.com> |
|---|---|
| First post | 2011-11-21 03:10 -0800 |
| Last post | 2011-11-21 20:36 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.c++
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
| From | avasilev <alxvasilev@gmail.com> |
|---|---|
| Date | 2011-11-21 03:10 -0800 |
| Subject | Protected member inaccessible from derived class - why |
| Message-ID | <90fac08a-2e86-4621-9b08-68e9d2f4d288@w7g2000yqc.googlegroups.com> |
Hi all,
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
Thanks in advance
Best regards
Alex
[toc] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-11-21 07:51 -0500 |
| Message-ID | <jadhhd$vo1$1@dont-email.me> |
| In reply to | #11658 |
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,
and not for any other object. IOW, in 'Derived::test' you should be
able to access 'this->a', but not 'anyotherBase.a'. That's just the
rule of the language.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | avasilev <alxvasilev@gmail.com> |
|---|---|
| Date | 2011-11-21 05:10 -0800 |
| Message-ID | <860e2edc-12e7-4f7a-9111-ef8b295838e6@o5g2000yqa.googlegroups.com> |
| In reply to | #11660 |
On Nov 21, 2:51 pm, Victor Bazarov <v.baza...@comcast.invalid> 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,
> and not for any other object. IOW, in 'Derived::test' you should be
> able to access 'this->a', but not 'anyotherBase.a'. That's just the
> rule of the language.
>
> V
> --
> I do not respond to top-posted replies, please don't ask
Thanks for the response, Victor
I thought about this, but I think I remember having written operator
methods, that contain something like:
MyClass& operator=(MyClass& other)
{
mMember = other.mMember;
...
}
where mMember was internal protected stuff. IS there any exception
for operators, or I am just mistaken?
Thanks
[toc] | [prev] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Date | 2011-11-21 16:41 +0100 |
| Message-ID | <jadrfk$2t7$1@dont-email.me> |
| In reply to | #11661 |
On 21.11.2011 14:10, avasilev wrote:
>>> void test(Base* b)
>
> MyClass& operator=(MyClass& other)
> {
> mMember = other.mMember;
> ...
> }
>
> where mMember was internal protected stuff. IS there any exception
> for operators, or I am just mistaken?
Can you see any obvious difference above?
Cheers & hth.,
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-11-21 18:40 +0000 |
| Message-ID | <LYudnaCQn4GFBlfTnZ2dnUVZ8nudnZ2d@giganews.com> |
| In reply to | #11660 |
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);
}
HTH.
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Paul" <pchrist<nospam>or@yahoo.co.uk> |
|---|---|
| Date | 2011-11-21 20:36 +0000 |
| Message-ID | <jaecot$2igm$1@adenine.netfront.net> |
| In reply to | #11667 |
"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 ---
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web