TITLE: accessing prot/private members via base class ptr/ref (Newsgroups: comp.std.c++, 14 May 99) MEB: MEB >Why is the language designed to forbid the following code: > >class A >{ >protected: > int i; >}; > >class B : public A >{ >public: > B( A &a) > { > i = a.i; // access to protected i in class A not allowed > } >}; CLAMAGE: Steve Clamage See the ARM, page 254. It would allow members of B to access any A object or any A subobject of an unrelated type: class C : public A { ... }; void B::foo(C* pc) { pc->A::i = 4; } The code modifies the protected state of an unrelated object. That isn't the purpose of protected access. The idea is that you can access the protected state of an object of your own type or a derived type, not of other types. This rule has been part of C++ as long as protected access has been in the language. It is still in the standard, section 11.5, paragraph 1.