Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Protected member inaccessible from derived class - why

Reply
Thread Tools

Protected member inaccessible from derived class - why

 
 
avasilev
Guest
Posts: n/a
 
      11-21-2011
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-21-2011
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
 
Reply With Quote
 
 
 
 
avasilev
Guest
Posts: n/a
 
      11-21-2011
On Nov 21, 2:51*pm, Victor Bazarov <(E-Mail Removed)> 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-21-2011
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can derived member function not access protected member of a baseclass object? blangela C++ 8 09-26-2008 03:24 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
When is a base class protected member not visible in a derived class? Andy Lomax C++ 5 06-30-2005 04:34 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Is this valid C++ (protected member of base accessed in derived class function) Shelly Adhikari C++ 3 09-10-2003 08:18 PM



Advertisments