无法从派生类访问基类中的受保护成员

Can't access protected member in base class from derived class

本文关键字:受保护 成员 基类 访问 派生      更新时间:2023-10-16

下面是我的代码:

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class root
{
    protected :
            int size;
            double *array;
    public :
        virtual ~root() {}
        virtual root* add(const root&) = 0;
        virtual root* sub(const root&) = 0;
        virtual istream& in(istream&, root&) = 0;
        virtual int getSize() const = 0;
        virtual void setSize(int);
};
class aa: public root
{
    public :
        aa();
        aa(int);
        aa(const aa&);
        root* add(const root& a);
        root* sub(const root& a);
        istream& in(istream&, root&){}
        int getSize() const;
        void setSize(int);
};
class bb: public root
{
public:
    bb() { }
    bb(const bb& b) { }
    root* add(const root& a);
    root* sub(const root& a);
    istream& in(istream&, root&){}
    int getSize() const{}
    void setSize(int){}
};
aa::aa()
{
    size = 0;
    array = NULL;
}
aa::aa(int nsize)
{
    size = nsize;
    array = new double[size+1];
    for(int i=0; i<size; i++)
        array[i] = 0;
}
root* aa::add(const root& a)
{
    for (int i=0; i<a.size; i++)
        array[i] += a.array[i];
    return *this;
}
root* aa::sub(const root& a)
{
}
int aa::getSize() const
{
    return size;
}
void aa::setSize(int nsize)
{
    size = nsize;
    array = new double[size+1];
    for(int i=0; i<size; i++)
        array[i] = 0;
}
root* bb::add(const root& a)
{
    return new bb();
}
root* bb::sub(const root& a)
{
}
int main(int argc, char **argv)
{
}

当我想访问sizearray在派生类,我只是不能,因为我的编译器说:

/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root* aa::add(const root&)’:|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: ‘int root::size’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|66|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘double* root::array’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|67|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|68|error: cannot convert ‘aa’ to ‘root*’ in return|
||=== Build finished: 5 errors, 0 warnings ===|

我读到受保护的成员在派生类中是私有的,所以它似乎是好的,但它不是。如何解决这个问题?

我读到受保护的成员在派生类中是私有的,所以它似乎是可以的,但它不是。

这并不是因为B (aa)从基类A(在本例中为root)继承的protected数据成员可以访问,只要它在类型为B (aa)的对象上被访问。这里,您通过类型为A (root)的对象访问它:
root* aa::add(const root& a)
{
    for (int i=0; i<a.size; i++)
    //              ^^^^^^
    //              Accessing size on an object of type `root`, not `aa`!
        array[i] += a.array[i];
    return *this;
}

c++ 11标准第11.4/1段:

当一个非静态数据时,在第11条前面描述的访问检查之外应用额外的访问检查成员或非静态成员函数是其命名类的受保护成员(11.2)。所述以前,授予对受保护成员的访问权限是因为引用发生在某个成员的友元或成员中类C 。如果访问要形成指向成员(5.3.1)的指针,则嵌套名称说明符应表示C或a所有其他访问都涉及(可能隐式的)对象表达式(5.2.5)。在这种情况下,对象表达式的类必须是C或从C派生的类。 [示例:

class B {
protected:
    int i;
    static int j;
};
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)
    j = 5; // OK (because j refers to static member)
    B::j = 6; // OK (because B::j refers to static member)
}

- end示例]

要解决这个问题,你需要提供公共setter/getter。您已经有了一个getSize()函数,所以不要这样写:

for (int i=0; i<a.size; i++)
//              ^^^^^^

你可以这样写:

for (int i=0; i<a.getSize(); i++)
//              ^^^^^^^^^^^

类似地,您必须提供用于获取/设置array的第n个元素的值的函数,以便您可以这样写:

array[i] += a.get_at(i);

注意,+=左边的表达式是OK的,因为array是通过this访问的(参见上面的c++ 11标准中的例子)。