Destructor being called twice when being explicitly invoked

后端 未结 10 466
孤街浪徒
孤街浪徒 2020-11-30 11:10

I was experimenting with destructors in C++ with this piece of code:

#include 

struct temp
{
    ~temp() { std::cout << \"Hello!\" <         


        
10条回答
  •  我在风中等你
    2020-11-30 11:51

    You shouldn't actually call the deconstructor. It is called for you by the runtime support. Hence your calling it once and the runtime support is calling it the second time.

    Here is some food for thought on destructors:

    http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr380.htm

    You can explicitly call deconstructors, however it's not recommended, normally they are implicitly called.

提交回复
热议问题