Scope with Brackets in C++

后端 未结 5 1867
滥情空心
滥情空心 2020-12-01 17:50

Is there any case in which putting code within brackets to reduce its scope is something that I might want to do, or is this one of those cases in which you guys will tell m

5条回答
  •  臣服心动
    2020-12-01 18:23

    For example, if you use the RAII idiom this may be useful. Synchronization locks for example.

    In most case, the scope of a method should be small enough for such locks. There are times when you want to limit the lock scope for either performance, or to avoid sending a gazillion parameters to a refactored method. Using this trick shouldn't be too common, though.

提交回复
热议问题