The point of abstraction of setters and getters is that the extraction method can change. When you access a member variable directly, then if something changes, you should update your usage everywhere. But if you use this function, then changing the function can only be changed in the function.
Now, since this is the same class, it is much less dangerous than when you make the variables public, because you only need to update this single implementation file (instead of updating each user of your class, which may not be possible if you have there is a library class).
To see this, look at a class that should suddenly become thread safe. Now your copier will look like
void myClass::copyVar1(myClass * dat) { scoped_lock lock(mutex); var1 = dat->getVar1(); }
and you don’t have to fix it anywhere if you always called the function. If you accessed a variable directly, it would look like
void myClass::copyVar1(myClass * dat) { scoped_lock lock(mutex); scoped_lock lock2(dat->mutex) var1 = dat->var1; }
which blocks the "mutex" dat, external to dat, is usually not considered a very good design (if you make engineers remember to block other people's objects, then you open the chance that they forget and do not).
Similarly, if a variable is stored in a database, you will also have to process it. Or, if now the variable was saved in two different variables and built when it was restored, you can see how complexity will increase.
But remember, the design philosophy for using accessories and mutators is to reduce potential complexity through encapsulation. There are times when you work on small projects (especially with personal projects) where you do not intend to change the class. Perhaps it would be less difficult to access them directly. Do not be afraid to do this, just know why you are making a decision.
ex0du5
source share