Protected Inheritance

Why can't this derived class access protected data from its base class?

1
2
3
4
5
6
7
8
9
10
class A {
protected:
	int n;
};

class B: public A {
	int f(B* b) {
		return n + b->n;
	}
};


1>c:\users\ben\documents\visual studio 2008\projects\test\main.cpp(8) : error C2248: 'A::n' : cannot access protected member declared in class 'A'
1>        c:\users\ben\documents\visual studio 2008\projects\test\main.cpp(3) : see declaration of 'A::n'
1>        c:\users\ben\documents\visual studio 2008\projects\test\main.cpp(1) : see declaration of 'A'
Edit: wait a second, this should work.
Is this really the exact code you're trying to compile? Are you perhaps trying to access n using a pointer to A?
Last edited on
OK thx.
b->n looks like trying to access a private member.
So even if the object is of our class type, it should still be illegal?

Edit: never mind.
Last edited on
Topic archived. No new replies allowed.