次のコードでは、コンパイラ エラーが発生します。
'BaseTest::_protMember': クラス 'BaseTest' で宣言された保護されたメンバーにアクセスできません
保護されているにもかかわらず、メンバー変数_protMember
にアクセスできないのはなぜですか?class SubTest
class BaseTest
{
public:
BaseTest(){};
BaseTest(int prot)
{
_protMember = prot;
};
protected:
int _protMember;
};
class SubTest : public BaseTest
{
// followup question
SubTest(const SubTest &subTest)
{
_protMember = subTest._protMember; // this line compiles without error
};
SubTest(const BaseTest &baseTest)
{
_protMember = baseTest._protMember; // this line produces the error
};
};
フォローアップの質問:
追加されたコピー コンストラクターで、別のインスタンスの保護されたメンバーにアクセスできるのはなぜですか?