c++ - Why not set class members to public in the first place? -
this question has answer here:
- why use getters , setters? 38 answers
i can't understand why people keep saying when make class should make get()
method variable in class, , set()
method set variable in class.
if have get()/set()
method... why not make variable public in first place?
it's not if accidentally change anyway since still need type myclassname.variable
am missing something?
using get/set functions gives flexibility change class implementation later without having change code accesses class.
for example, might change type of 1 of internal variables, or want add restrictions on values variable can set to. perhaps might want calculate value on demand.
also, can make get/set function non-inline, , put breakpoint on it.
Comments
Post a Comment