Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11660

Re: Protected member inaccessible from derived class - why

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Protected member inaccessible from derived class - why
Date 2011-11-21 07:51 -0500
Organization A noiseless patient Spider
Message-ID <jadhhd$vo1$1@dont-email.me> (permalink)
References <90fac08a-2e86-4621-9b08-68e9d2f4d288@w7g2000yqc.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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