【发布时间】:2017-10-15 05:07:49
【问题描述】:
Visual Studio 告诉我我无法从父类的实例中访问成员变量。像这样的:
class Point{
protected:
int x, y;
};
class Quadrilateral : public Point
{
protected:
Point A, B, C, D;
};
class Trapezoid : public Quadrilateral
{
public:
bool isTrapezoid() {
if (A.y == B.y && C.y == D.y)
return true;
return false;
}
};
据说Point::y 不能通过指针或对象访问。
谁能告诉我为什么?
【问题讨论】:
-
请贴出真实代码。
-
另外,“公共”继承(谢天谢地)不是真实的。
-
公共职能总是有点棘手! ;-)
-
在 Quadrilateral 中不需要继承 Point 类。无论如何,您都可以使用点对象。和受保护的成员可以从类和派生类访问。所以你可能需要公开 x,y
-
也许friend classes 就是您要找的。span>
标签: c++ inheritance protected