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