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

A

Andy Lomax

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();
}
----------------------------------------------------------
 
V

Victor Bazarov

Andy said:
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
 
A

Allan Bruce

Andy Lomax said:
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
 
A

anthonyhan

Another way.

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

John Carson

anthonyhan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top