If a variable has getter and setter, should it be public?

Posted by Oni on Programmers See other posts from Programmers or by Oni
Published on 2011-07-31T21:52:41Z Indexed on 2012/09/26 21:49 UTC
Read the original article Hit count: 217

Filed under:

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!

© Programmers or respective owner

Related posts about c++