I'm a total newbie with tons of ?'s in my mind and a lot to experience with C++ yet! There's been something which I find really confusing and it's the use of public variables, I've seen tons of code like this:
class Foo {
private:
int m_somePrivateVar;
public:
void setThatPrivateVar (int const & new_val) {
m_somePrivateVar = new_val;
}
int getThatPrivateVar (void) const {
return m_somePrivateVar;
}
};
Why would anyone hide that variable and implement accessors and mutators when there's nothing done in them more than assigning the new value just as it got received (no range checking etc.) or returning the value without just as it is? Well I've heard some reasons and some of them are convincing in some cases, but imagine implementing a huge class in such a manner for with a lot of variables which do not need any checking and stuff! Let me ask you this way, When do you use public variables? Do you use that at all?
See Question&Answers more detail:os