我想知道C++运行时系统如何检测对象何时超出范围,以便
它相应地调用析构函数以释放占用的内存。

谢谢。

最佳答案

这在编译时是静态已知的

{
  string s; /* ctor called here */
} /* dtor called here */

有时候比较困难
{
  again:
  {
    string s; /* ctor called here */
    goto again; /* now dtor of s is called here */
    string q; /* ctor not called. not reached. */
  } /* dtor of s and q would be called here. but not reached */
}

关于c++ - C++运行时系统如何知道对象何时超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5445070/

10-09 23:00