Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用像 A 类这样的类的全局实例;一个 a1; 现在我在其他函数中使用这个全局变量,使用引用为 A& a2 = a1; 这样在本地完成的所有更改都会在全局范围内反映出来。我只是想检查 a2 的析构函数是否会在其本地范围结束时被调用?
请澄清。
a2 是一个引用,所以它没有析构函数。
a1 的析构函数只会在程序退出时被调用。当 a2 的本地范围结束时,您究竟期望发生什么?
引用只不过是指针,实际上许多编译器在内部将引用转换为指针。由于指针没有析构函数,所以引用也没有。因此,在函数的末尾,gobal 对象 a1 将保持原样,并且它的析构函数只会在程序结束时被调用。
谢谢尼拉吉·拉蒂