You can only access protected members from your own instance of the base class ... none of them are provided to you as a parameter. It's all about encapsulating OO. Without this restriction, an object under construction can invalidate the invariants of the baseTest& parameter.
In other words, your SubTest may decide to use for a protected member that conflicts with using the same member with another BaseTest -derived class (say SubTest2 : BaseTest ). If your SubTest code SubTest allowed to play with other object data, this can invalidate the invariants in the SubTest2 object or get some values ββthat were - in the intended encapsulation - only to be exposed to SubTest2 and (optionally, see below) SubTest2 derivatives.
Follow-up question: Why is it that in the added copy constructor I can access the protected members of another instance?
SubTest(const SubTest& x);
The same thing described above explains why this is allowed: the copy constructor gets SubTest& , and not just any old object obtained from BaseTest , and this constructor is explicitly in the SubTest abstraction. It is assumed that the SubTest encoder is familiar with the intended construction / encapsulation of SubTest , and the copy constructor is granted access to bypass and enforce post-conditions / invariants on another SubTest& object. (You are copying an object, which itself can be replicated by the same function, so protecting it, if on the *this side, but not on the parameter side, is not significant protection, even ignoring all the reasons why you may need / need this access).
It is possible that a SubTest -adjusted object will be accidentally passed to the SubTest copy SubTest ("cut"), but even for this scenario, the SubTest& class can control whether the object can be done unexpectedly with _protMember - by adding a private statement using BaseTest::_protMember; if he wants to "finalize" access to _protMember and prohibit the use of any derived classes.
Tony delroy
source share