The following code produces a compiler error:
\'BaseTest::_protMember\' : cannot access protected member declared in class \'BaseTest\'
You can access protected
members only in the class instance. That is :
class SubTest : public BaseTest
{
SubTest(const BaseTest &baseTest)
{
_protMember = baseTest._protMember;
// ^^^^^^^^^^^ Is good because you are in the instance of its class
_protMember = baseTest._protMember;
// ^^^^^^^^^^^^^^^^^^^^^ Produce error because you are not in the baseTest instance.
};
// followup question
SubTest(const SubTest &subTest)
{
_protMember = subTest._protMember;
// Compile because access modifiers work on class level, and not on object level.
};
};
EDIT for the followup :
The access modifiers work on class level, and not on object level.
That is, two objects of the same class can access each others private members.
This is my source : Why can I access private variables in the copy constructor?