1. #include <iostream>: 包含了输入输出流的头文件。
  2. #include <exception>: 包含了异常处理相关的头文件。
  3. using namespace std;: 使用了命名空间std,这样可以直接使用std中的函数和类,而不需要加上std::。
  4. class Dog: 定义了一个名为Dog的类。
  5. Dog(): Dog类的构造函数,输出"Dog"。
  6. ~Dog(): Dog类的析构函数,输出"~Dog"。
  7. } wc;: 创建了一个名为wc的Dog类的对象。
  8. int main(): 程序的主函数。
  9. cout << "in main" << endl;: 输出"in main"。
  10. int *ptr = new int [1000];: 动态分配了一个大小为1000的int数组,并将其地址赋给了指针ptr。
  11. Dog *pdg = new (ptr) Dog;: 在ptr指向的内存地址上构造了一个Dog类的对象,并将其地址赋给了指针pdg。
  12. cout << pdg << " " << ptr << endl;: 输出pdg和ptr指向的内存地址。
  13. pdg->~Dog();: 调用Dog类的析构函数。
  14. pdg = nullptr;: 将指针pdg置为空指针。
  15. delete [] ptr;: 释放了之前动态分配的int数组的内存。
  16. return 0;: 返回0,表示程序正常结束
#include iostream#include exceptionusing namespace std;class Dogpublic	Dog 		cout Dog endl;		~Dog 		cout ~Dog endl;	wc;int main	cout in main endl;	int ptr = new int 1000;	Dog pdg = new ptr Dog;

原文地址: https://www.cveoy.top/t/topic/iLGQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录