If a variable has getter and setter, should it be public?
- by Oni
I am an about to graduate Computer Science student so probably this is a stupid question.
If I have a class with a variable that is private and the class have getter and setter for that variable. Why don't make that variable public?
The only case I think you have to use getters and setters is if you need to do some operation besides the set or the get. Example:
void my_class::set_variable(int x){
/* Some operation like updating a log */
this->variable = x;
}
Thanks in advance!