1.RAII:资源申请即初始化:#define _CRT_SECURE_NO_WARNINGS#include #include #include using namespace std;class mystr{public:char *p = nullptr;public:mystr(const char *str){cout << "构建" << endl;int length = strlen(str);p = new char[length + 1];strcpy(p, str);p[length] = '\0';}~mystr(){cout << "销毁" << endl;delete[] p;}};void go(){char *p = new char[100];//RAII 避开内存泄露,一般情况下,堆上的内存当作栈上来使用//栈内存有限,希望自动释放,用很大的内存。mystr str1 = "ABCD"; }void main(){go();cin.get();}运行结果:构建销毁