Scope with Brackets in C++

后端 未结 5 1869
滥情空心
滥情空心 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:32

    I have found another use case in my own code: unit testing destructors. Example using UnitTest++ (but the same principle applies regardless of unit testing framework):

    TEST(SomeTest)
    {
       {
       SomeClass someObject;
       } // someObject is destroyed
       CHECK(isSomeClassDestructorWorking())
       // e.g. have all resources been freed up?
    }
    

提交回复
热议问题