Destructor being called twice when being explicitly invoked

后端 未结 10 461
孤街浪徒
孤街浪徒 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:36

    The destructor is not the "destroyer" of the object. It's just an ordinary function, but it's called automatically by the language immediately prior to the time of destruction.

    It's official name is the destructor, but perhaps it would be more easily understood if we called it the "Before-Destruction" function.

提交回复
热议问题