I find the surprising. If I derive Rectangle from Point, I can access the
members of Point inherited by Rectangle _IF_ they are actually members of a
Rectangle. If I have a member of type Point in Rectangle, the compiler
tells me Point::x is protected. I would have expected Rectangle to see the
protected members of any Point. Compiling the following code give me this
error:
g++ -o rectangle main.cc
main.cc: In member function `size_t Rectangle::dx()':
main.cc:22: error: `size_t Point::y' is protected
main.cc:32: error: within this context
#include <iostream>
#include <cstddef> //size_t
class Point {
public:
Point(const size_t& x_=0,
const size_t& y_=0)
: x(x_),
y(y_)
{}
Point& operator+(const Point& p)
{
this->x += p.x;
this->y += p.y;
return *this;
}
protected:
size_t x;
size_t y; //line 22
};
class Rectangle : public Point {
public:
Rectangle(const Point& xy, const Point& dxdy_)
: Point(xy),
dxdy(dxdy_)
{}
size_t X() const { return this->x; }
size_t dx() const { return this->dxdy.y; } //line 32
protected:
Point dxdy;
};
int main(){
}
ISO/IEC 14882 says this:
11.2 Accessibility of base classes and base class members
[class.access.base]
"If a class is declared to be a base class (clause 10) for another class
using the public access specifier, the public members of the base class are
accessible as public members of the derived class and protected members of
the base class are accessible as protected members of the derived class."
The way I read that, I should be able to access Rectangle::dxdy.y from
within Rectangle. Am I missing something, or is g++ wrong?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org