为什么派生类不能通过指向base的指针访问其基类的受保护成员? [英] Why can a derived class not access a protected member of its base class through a pointer to base?

查看:766
本文介绍了为什么派生类不能通过指向base的指针访问其基类的受保护成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

class TestA
{
protected:
    int test=12;

public:
    TestA() {
        cout << "test a: " << test << endl;
    }
    ~TestA() {
    }
};

class TestB : public TestA
{   
public:
    TestB(TestA *testA) {
        cout << "test b: " << testA->test;
    }
    ~TestB() {
    }
};

int main ()
{
    TestA *pTestA=new TestA();
    TestB *pTestB=new TestB(pTestA);
}



我试图访问 / code>成员使用指向 TestA 类型对象的指针(因此, TestA ) 。 TestB 也来源于 TestA

I'm trying to access of a protected member using a pointer pointing to a TestA type object (thus, an instance of TestA). TestB is also derived from TestA

不能访问它?是否只能访问在我需要它的类中?

Why I can't access to it? Is it accessible only "within" the class where I need it? Not outside using pointer/direct declarations?

推荐答案

当公共继承自基类时,其受保护的成员变成类'保护成员,这些成员可以在派生类的成员函数中访问。但是它们只能通过派生类本身(及其派生类)访问,无法通过基类访问。因此,您不能通过 TestA 的指针访问成员 test ,但通过指针访问它会很好 TestB

When public inherite from the base class, its protected members become the derived class' protect members, which could be accessed in derived class' member functions. But they could only be accessed through the derived class itself (and its derived classes), can't be accessed through the base class. So you can't access member test via pointer of TestA, but it'll be fine to access it via pointer of TestB.

标准给出了一些说明性的例子。 $ 11.4 / 1受保护的成员访问权限[class.protected]

The standard gives some illustrative samples for this. $11.4/1 Protected member access [class.protected]:

(只保留示例代码的一部分)

(Only keep a part of the example code)


更早的条款
[class.access]在非静态数据成员或非静态
成员函数是其命名类的受保护成员时应用
([class.access.base ])114如前所述,授予对受保护的
成员的访问权,因为引用发生在某个类C的朋友或成员
中。如果访问是形成指向成员
的指针([expr.unary.op]),嵌套名称说明符应表示C或从C派生的类
。所有其他访问涉及一个(可能是隐式)
对象表达式([expr.ref ])。在这种情况下,对象
表达式的类应为C或从C派生的类。[示例:

An additional access check beyond those described earlier in Clause [class.access] is applied when a non-static data member or non-static member function is a protected member of its naming class ([class.access.base])114 As described earlier, access to a protected member is granted because the reference occurs in a friend or member of some class C. If the access is to form a pointer to member ([expr.unary.op]), the nested-name-specifier shall denote C or a class derived from C. All other accesses involve a (possibly implicit) object expression ([expr.ref]). In this case, the class of the object expression shall be C or a class derived from C. [ Example:

class B {
protected:
  int i;
};

class D1 : public B {
};

class D2 : public B {
  void mem(B*,D1*);
};

void D2::mem(B* pb, D1* p1) {
  pb->i = 1;                    // ill-formed
  p1->i = 2;                    // ill-formed
  i = 3;                        // OK (access through this)
  B::i = 4;                     // OK (access through this, qualification ignored)
}

/ p>

— end example ]

我不确定你的设计的意图,使 TestB code> TestA 将是一个简单的解决方案。

I'm not sure about your design's intent, making TestB friend of TestA would be a straightforward solution.

这篇关于为什么派生类不能通过指向base的指针访问其基类的受保护成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆