static variable vs. member

前端 未结 3 1191
轻奢々
轻奢々 2021-02-07 17:25

If you have data for a class that will be modified and needs to be retained throughout the program, but is only used in one member function, is it preferred to make that variabl

3条回答
  •  别跟我提以往
    2021-02-07 17:55

    Declaring a local variable as static means your method now has state, separate from the object's state. It can lead to many mistakes when maintaining this code (such as copy constructor implementation, assignment, serialization) and when reading it (unclear method behavior).
    Avoid using static locals unless you have some good reason (the only one I can think of is single threaded singletone implementation).

提交回复
热议问题