What's your take on protected variables?
In the context of c++, it seems most experts (Scott Meyes, Herb Sutter) think is not a good idea. I don't agree. In fact, some languages, for example Ruby, have only automatic protected variables.
It seems silly to make a 'setter' and 'getter' functions for a variable when the derived object is employ in terms of "is-a". Also, making 'setter' functions opens the variable to be manipulated from outside the object! Of course, one could make in the 'setter' protected, but isn't that 'running in a loop'?
Please, discuss.
EDIT:
Let's say I have:
[source lang="cpp"]class Shape{public: virtual void CalculateArea();private: //private! int x, y; int width, height;};class Rectangle : public Shape{public: void CalculateArea(){ /* I need width and height to calculate, but can't access it. What can I do? */ }};[/source]
If I make 'getters' for Shape class:
[source lang="cpp"]class Rectangle : public Shape{public: void CalculateArea(){ get_width(); //isn't this silly, as width is a fundamental part of Rectangle //also, now everyone knows my width! }};[/source]
the problems with getters and setters is, people take them too literally.
they make a get and set for each variable, allowing users to, again, invalidate state.
your rectangle class needs left, right, width, top, bottom, height, area, aspectratio, etc to be publically accessible. and none of those getters/setters/whatevers should make it possible to have an invalid rectangle, ever.
set_width() can check if you enter a negative width, and prevent it. set_right can check if your right is more left than your left, and prevent it. and vice versa.
it's not about making methods that directly expose your variables in public. if you want that, use public directly. getters and setters is about NOT exposing anyting directly. aobut making SMART getters and setters. in that case, about making a rectangle ALWAYS a rectangle. not something that has negative space, or what ever.
and as having an object valid at all time is very important, public variables are dangerous. as are protected ones.
but forget about getters and setters. don't write a car that has get_position and set_position. write a car that can drive(), turn(), break() and return current_position(), current_orientation() instead.
if you expose your variables directly, you did not understand the idea of getters and setters.

Find content
Not Telling