1 #include <iostream> 2 3 using namespace std; 4 // 5 ////c语言版本 6 //struct stu 7 //{ 8 // char *name; 9 // int age; 10 //}; 11 //int main() 12 //{ 13 // stu *ps = (stu *)malloc(sizeof(stu)); 14 // ps->name = (char *)malloc(1000);//注意上面一句完了必须用这一句 name才会右空间 15 // strcpy(ps->name,"dfd"); 16 // 17 // //free(ps);//不能直接free 18 // free(ps->name); 19 // free(ps); 20 // return 1; 21 //} 22 23 //c++多简洁 24 class stu 25 { 26 public: 27 stu() 28 { 29 name = new char[1000]; 30 } 31 ~stu() 32 { 33 delete []name; 34 } 35 public: 36 char *name; 37 int age; 38 }; 39 40 int main() 41 { 42 stu *ps = new stu; 43 44 strcpy(ps->name,"dfasdf"); 45 delete(ps); 46 return 1; 47 }