Greg wrote:
Bleargh, my C++ hateometer is at 8.6, that design is cac. So how do I
derive a class that can access the private members ? Or how can I make
the same overall design and avoid this dilema ?
If it's private, then its up to the base class to give you access to
it. Why is it that you beleive that you can't access it?
class a
{
int y;
public:
void setY(int n) { y = n; }
int getY() const { return y; }
}; <- semicolon! its a class declaration
class b : public a
{
public:
void b::somemethod() const;
};
void b::somemethod()
{
setY(20);
}
Now consider a situation where you need to only allow y to be between 0
and 20, as an example. Why should the programmer be forced to have
those classes NOT responsible for the variable check that?
The point is that if y is protected, then class b must also impose the
0 <= y <= 20 verification. Thats would be a dumb strategy. The fact
that y above is private is helping the creator of the class determine
the rules of the design. Can you see that even an instance of class b
can now set/get y?
int main()
{
b bill;
bill.setY(11);
}