Skip to main content
3 of 3
C++ tag not necessary
Ampt
  • 4.7k
  • 2
  • 25
  • 47

Why shouldn't I make variables public, but should use public getters/setters?

I'm watching a C++ tutorial video. It is talking about variables inside classes and assert that variables should be marked private. It explains that if I want to use them publicly, I should do it indirectly through functions.

Why? What's the difference? It looks like the result is the same, except that instead of having one line of code, I now have like 9 or 10 more LOC to do the same thing.

Can someone explain why converting a private variable in a class to public within the class rather than just making it public right out the simple way is any different and more efficient?