What good are public variables then?

后端 未结 14 2250
悲哀的现实
悲哀的现实 2020-12-09 15:16

I\'m a total newbie with tons of ?\'s in my mind and a lot to experience with C++ yet! There\'s been something which I find really confusing and it\'s the use of public vari

14条回答
  •  萌比男神i
    2020-12-09 15:28

    One argument runs something like this:

    If code external to the class in question relies directly on a particular member, making a change to that member means changing every piece of code that accesses it. If instead it is accessed by member functions, you can keep that part of the interface the same, and only have to make changes to that class to keep external code working.

    Using "getters" and "setters" gives you some flexibility built into the coupling of your objects.

提交回复
热议问题