Jim Langston wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:e4uvmj$np6$1@news.datemas.de...[color=green]
>>
olanglois@sympatico.ca wrote:[color=darkred]
>>> I am not sure if I have found a compiler bug (I am using
>>> VC++.NET2003)[/color]
>>
>> No.
>>[color=darkred]
>>> or if this is the correct behavior defined by the language[/color]
>>
>> Yes.
>>[color=darkred]
>>> but I am
>>> sure someone can clear up my confusion. Suppose the following:
>>>
>>> class Base
>>> {
>>> protected:
>>> int x;
>>> };
>>>
>>> class Derived : public Base
>>> {
>>> public:
>>> void A( const Derived &d )
>>> { x = d.x; // Ok }
>>> void B( const Base &b )
>>> { x = b.x; //Error: cannot access protected member declared in
>>> class 'Base' }[/color]
>>
>> Correct behaviour.
>>[color=darkred]
>>> };
>>>
>>> What is the problem with B()? What should I change in Derived to fix
>>> the problem?[/color]
>>
>> Nothing. The derived class cannot access protected members in
>> objects of other than its own type.[/color]
>
> I was curious about this and looked up the protected keyword in MSDN
> and came across this:
>
> The protected keyword specifies access to class members in the
> member-list up to the next access specifier (public or private) or
> the end of the class definition. Class members declared as protected
> can be used only by the following:
>
> Member functions of the class that originally declared these members.
> Friends of the class that originally declared these members.
> Classes derived with public or protected access from the class that
> originally declared these members.
> Direct privately derived classes that also have private access to
> protected members.
>
> Is MSDN wrong or am I reading it wrong?[/color]
MSDN's description is incomplete. Does it mean it's wrong? I don't
know.
An object can only access protected members of the object of its own
type. From 11.5/1:
"When a friend or a member function of a derived class references
a protected nonstatic member function or
protected nonstatic data member of a base class, an access check
applies in addition to those described earlier
in clause 11.102) Except when forming a pointer to member (5.3.1),
the access must be through a
pointer to, reference to, or object of the derived class itself
(or any class derived from that class) (5.2.5)."
See the "must be through a pointer to, reference to, or object of
the defiend class itself"?
[color=blue]
> It seems to state that
> "...can be used only by the following: ... Classes derived with
> public or protected access from the class that originally declared
> these members..."
> Isn't that the case here? Isn't Derived deriving with public access
> from Base? I'm not sure since I never use protected.[/color]
It's not the case here. The object '*this' is trying to access the
'x' protected member in an object of type 'Base', IOW in an object
of another type than its own.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask