G
gw7rib
I'm getting in knots about accessing a protected member. I was under
the impression that (contrary to what one might expect) access to
members is worked out on a per-class basis rather than a per-object
basis. But I'm still having a problem. The following code illustrates
it:
void use(int val);
class Base {
protected:
int val;
public:
void foo(Base *other);
};
void Base::foo(Base *other) {
use(other -> val); /* Line 1 - works */
}
class Derived : public Base {
public:
void bar(Base *other);
};
void Derived::bar(Base *other) {
use(val); /* Line 2 - works */
use(other -> val); /* Line 3 - doesn't work */
}
At line 1, I am accessing a member of another object, but this is
allowed as it is the same class and it is the class, rather than the
specific object, that matters.
At line 2, I am accessing a protected member of the base class. This
works - it is an example of what "protected" means.
But line 3 does not work, even though I am allowed to access the val
member of the base class of the same object (see line 2) and (see
line 1) the actual object shouldn't matter for acessing purposes. What
is this line doing wrong that the other two aren't?
Thanks for any explanation.
Paul.
the impression that (contrary to what one might expect) access to
members is worked out on a per-class basis rather than a per-object
basis. But I'm still having a problem. The following code illustrates
it:
void use(int val);
class Base {
protected:
int val;
public:
void foo(Base *other);
};
void Base::foo(Base *other) {
use(other -> val); /* Line 1 - works */
}
class Derived : public Base {
public:
void bar(Base *other);
};
void Derived::bar(Base *other) {
use(val); /* Line 2 - works */
use(other -> val); /* Line 3 - doesn't work */
}
At line 1, I am accessing a member of another object, but this is
allowed as it is the same class and it is the class, rather than the
specific object, that matters.
At line 2, I am accessing a protected member of the base class. This
works - it is an example of what "protected" means.
But line 3 does not work, even though I am allowed to access the val
member of the base class of the same object (see line 2) and (see
line 1) the actual object shouldn't matter for acessing purposes. What
is this line doing wrong that the other two aren't?
Thanks for any explanation.
Paul.