//匿名对象的生命周期
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Point{
public:
    Point(){
        cout << "自定义的无参构造函数被调用了1" << endl;
    }
    ~Point(){
        cout << "自定义的析构函数被调用了2" << endl;
    }
};

void ProtectA(){
    //直接调用Point的类构造函数
    Point();
    //此时c++编译器会自动生成匿名对象,
    //但是通过观察发现  执行Point();同时调用无参构造函数和析构函数
    //说明c++编译器发现后面没有代码调用这个匿名对象,所以立刻释放了
}

void main(){
    ProtectA();

    system("pause");
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2021-06-20
  • 2021-12-08
  • 2021-08-31
  • 2021-07-19
猜你喜欢
  • 2022-12-23
  • 2021-09-13
  • 2021-12-22
  • 2022-12-23
相关资源
相似解决方案