C++类访问级别

J. *_*Lin 9 c++ inheritance

假设我有两个班级.一个叫Point:

class Point{
    public:
        Point(): x_(0), y_(0) {}
    protected: 
        int x_, y_;
    };
Run Code Online (Sandbox Code Playgroud)

然后我有另一个类,它派生自Point:

class Point3D : public Point {
public:
    Point3D(): Point(), z_(0){}
    double distance(Point3D pt, Point base) const;
protected:
    int z_;
};

double Point3D::distance(Point3D pt, Point base) const
{
    int dist_x, dist_y;
    dist_x = base.x_ - x_;
    dist_y = base.y_ - y_;

    return sqrt(dist_x * dist_x +
                dist_y * dist_y);
}
Run Code Online (Sandbox Code Playgroud)

然后我得到了如下错误:base.x_在此上下文中受到保护.但Point3D到Point的访问级别是公共的,并且Point中的x_ data成员受到保护.所以它应该没有错误,对吧?有人可以帮我解决这个问题吗?

R S*_*ahu 6

但是的访问级别Point3DPointpublic

这并非完全正确.仅当派生类通过派生类指针/引用访问时,才能访问基类的受保护成员.

double Point3D::distance(Point3D pt, Point base) const
{
    int dist_x, dist_y;
    dist_x = base.x_ - x_; // x_ is same as this->x_. Hence it is OK.
                           // base is not a Point3D. Hence base.x_ is not OK.
    dist_y = base.y_ - y_;

    return sqrt(dist_x * dist_x +
                dist_y * dist_y);
}
Run Code Online (Sandbox Code Playgroud)

允许protected通过基类指针/引用访问基类的成员将允许您以导致意外后果的方式更改对象.

说你有:

class Base
{
   protected:
     int data;
};

class Derived1 : public Base
{
    void foo(Base& b)
    {
       b.data = 10; // Not OK. If this were OK ... what would happen?
    }
};

class Derived2 : public Base
{
};

int main()
{
    Derived1 d1;
    Derived2 d2;
    d1.foo(d2); 
}
Run Code Online (Sandbox Code Playgroud)

如果

       b.data = 10;
Run Code Online (Sandbox Code Playgroud)

如果被允许进入Derived1::foo,你就可以修改by的d2一个实例的状态.这种后门修改不是一种理想的行为.Derived2Derived1