Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > When is a base class protected member not visible in a derived class?

Reply
Thread Tools

When is a base class protected member not visible in a derived class?

 
 
Andy Lomax
Guest
Posts: n/a
 
      06-29-2005
Can anyone tell me why the code below doesn't compile?

The code has a simple hierarchy of publically-derived classes: A -> B
-> C. A declares a protected member 'foo'. C declares an object of
type B, and attempts to access B.foo, which results in a compiler
error:

test.cpp: In member function `void C::test()':
test.cpp:5: error: `int A::foo' is protected
test.cpp:15: error: within this context

Cheers -

AL

----------------------------------------------------------
#include <iostream>

class A {
protected:
int foo;
};

class B: public A {
};

class C: public B {
public:
void test() {
B b;
std::cout << b.foo << std::endl;
}
};

int main() {
C c;
c.test();
}
----------------------------------------------------------
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-29-2005
Andy Lomax wrote:
> Can anyone tell me why the code below doesn't compile?


See below.

> The code has a simple hierarchy of publically-derived classes: A -> B
> -> C. A declares a protected member 'foo'. C declares an object of
> type B, and attempts to access B.foo, which results in a compiler
> error:
>
> test.cpp: In member function `void C::test()':
> test.cpp:5: error: `int A::foo' is protected
> test.cpp:15: error: within this context


Yes. The cause: a common misconception of what 'protected' is for.

>
> Cheers -
>
> AL
>
> ----------------------------------------------------------
> #include <iostream>
>
> class A {
> protected:
> int foo;
> };
>
> class B: public A {
> };
>
> class C: public B {
> public:
> void test() {
> B b;
> std::cout << b.foo << std::endl;


Inside a C object you're only allowed to access protected members of
_your_own_ instance (through this->) or of another instance of type C.
This should be OK:

this->foo;
C c;
c.foo;

> }
> };
>
> int main() {
> C c;
> c.test();
> }
> ----------------------------------------------------------


V
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      06-29-2005

"Andy Lomax" <abuse@[127.0.0.1]> wrote in message
news:(E-Mail Removed)...
> Can anyone tell me why the code below doesn't compile?
>
> The code has a simple hierarchy of publically-derived classes: A -> B
> -> C. A declares a protected member 'foo'. C declares an object of
> type B, and attempts to access B.foo, which results in a compiler
> error:
>
> test.cpp: In member function `void C::test()':
> test.cpp:5: error: `int A::foo' is protected
> test.cpp:15: error: within this context
>
> Cheers -
>
> AL
>
> ----------------------------------------------------------
> #include <iostream>
>
> class A {
> protected:
> int foo;
> };
>
> class B: public A {
> };
>
> class C: public B {
> public:
> void test() {
> B b;
> std::cout << b.foo << std::endl;
> }
> };
>
> int main() {
> C c;
> c.test();
> }
> ----------------------------------------------------------


As Victor said, protected is for internal access - if you want to have
external access like you have then you can use the 'friend' keyword.

Allan


 
Reply With Quote
 
Andy Lomax
Guest
Posts: n/a
 
      06-29-2005
On Wed, 29 Jun 2005 11:08:33 -0400, Victor Bazarov
<(E-Mail Removed)> wrote:

>Andy Lomax wrote:
>> Can anyone tell me why the code below doesn't compile?

>
>Yes. The cause: a common misconception of what 'protected' is for.


Great.

Thanks -

AL
 
Reply With Quote
 
anthonyhan
Guest
Posts: n/a
 
      06-30-2005
Another way.

class C: public B {
public:
void test() {
std::cout << B::foo << std::endl;
}

 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      06-30-2005
"anthonyhan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> Another way.
>
> class C: public B {
> public:
> void test() {
> std::cout << B::foo << std::endl;
> }


That is equivalent to

class C: public B {
public:
void test() {
std::cout << foo << std::endl;
}

It accesses the protected member in the object's *own* B subobject and
doesn't address the original issue, which is accessing a protected member in
*another* instance of B.


--
John Carson

 
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
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Type names defined in base template class not visible in derived class in gcc3.2? James Ying C++ 2 10-28-2003 11:36 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