Is a C++ destructor guaranteed not to be called until the end of the block?

后端 未结 8 1951
孤街浪徒
孤街浪徒 2021-01-03 18:03

In the C++ code below, am I guaranteed that the ~obj() destructor will be called after the // More code executes? Or is the compiler allowed to destruct the

相关标签:
8条回答
  • 2021-01-03 18:42

    The destructor will not be called until the object goes out of scope.

    The C++ faq lite has a good section on dtors

    0 讨论(0)
  • 2021-01-03 18:43

    You are OK with this - it's a very commonly used pattern in C++ programming. From the C++ Standard section 12.4/10, referring to when a destructor is called:

    for a constructed object with automatic storage duration when the block in which the object is created exits

    0 讨论(0)
提交回复
热议问题