#include <iostream>
using namespace std;
class Test {
public:
Test() {
p = (char*)malloc(512);
if (p != NULL) strcpy_s(p, "11111");
cout << "我是构造函数,自动被调用了" << endl;
}
~Test() {
cout << "我是析构函数,自动被调用了" << endl;
if (p != NULL) {
cout << "free(p)" << endl;
free(p);
}
}
protected:
private:
int a;
char* p;
};
void test() {
Test t, t1;
cout << "展示t生命周期" << endl;
}
void main()
{
test();
system("pause");
}
补充start:C++构造和析构
![](https://img-home.csdnimg.cn/images/20240711042549.png)