7

这是我的代码:

#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 ===|

我读到受保护的成员在派生类中是私有的,所以看起来没问题,但事实并非如此。如何解决这个问题?

4

1 回答 1

7

我读到受保护的成员在派生类中是私有的,所以看起来没问题,但事实并非如此。

这并不是因为派生类(在这种情况下)protected从基类A(在这种情况下)继承的数据成员是可访问的,只要它在()类型的对象上被访问。在这里,您通过( )类型的对象访问它:rootBaaBaaAroot

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.2) 的受保护成员时,将应用超出第 11 节中所述的附加访问检查。如前所述,授予对受保护成员的访问权限是因为引用发生在某个朋友或某个类 C 的成员中。如果访问要形成指向成员的指针(5.3.1),则嵌套名称说明符应表示 C 或从 C 派生的类。所有其他访问都涉及(可能是隐式的)对象表达式(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)
}

—结束示例]

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

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

你可以这样写:

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

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

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

请注意,左侧的表达式+=是可以的,因为array正在通过访问this(另请参见 C++11 标准中的上述示例)。

于 2013-05-09T10:35:15.733 回答