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

后端 未结 8 1956
孤街浪徒
孤街浪徒 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:28

    A typical example of this, just as your question is the boost::scoped_ptr (or similar std::auto_ptr) :

    {
        boost::scoped_ptr< MyClass > pMyClass( new MyClass );
    
        // code using pMyClass here
    
    } // destruction of MyClass and memory freed
    

提交回复
热议问题