Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Protected member inaccessible from derived class - why (http://www.velocityreviews.com/forums/t806137-protected-member-inaccessible-from-derived-class-why.html)

avasilev 11-21-2011 11:10 AM

Protected member inaccessible from derived class - why
 
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

Victor Bazarov 11-21-2011 12:51 PM

Re: Protected member inaccessible from derived class - why
 
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

avasilev 11-21-2011 01:10 PM

Re: Protected member inaccessible from derived class - why
 
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



Alf P. Steinbach 11-21-2011 03:41 PM

Re: Protected member inaccessible from derived class - why
 
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




All times are GMT. The time now is 01:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57