What good are public variables then?

后端 未结 14 2228
悲哀的现实
悲哀的现实 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 15:41

    I don't care much for writing accessors that do nothing. I have two reasons for this attitude.

    • One of the reasons for doing so is that the code will later be inherited from. I've found this is usually not true in practice. What usually happens is the code is refactored or replaced. The lifespan of code is almost always very short.

    • You're making extra work now for something that might, or might not, happen in the future. This is a variation of the "premature optimization" fault.

    There are places where it's a good idea and places where it's not. In most cases I feel the answer is it's not useful.

提交回复
热议问题