赞
踩
-
-
- Student stu; //对象已被实例化,已分配内存空间,可以使用了stu.say(); //调用成员函数
new Student;也可以使用构造函数:
new Student("小明", 15, 90.5f);这样,就在堆区为对象分配了内存,并调用了构造函数。
Student *pStu; pStu = new Student("小明", 15, 90.5f);或者:
Student *pStu = new Student("小明", 15, 90.5f);
delete pStu;这样,就释放掉了对象占用的内存,并调用了析构函数。
-
-
- #include <iostream>#include <cstdlib>using namespace std;class Demo{private: double n; double m; int i;};void func(){ Demo *p = new Demo;}int main(){ int i; for(i=1; i<=1000000; i++){ func(); } system("pause"); return 0;}
-
-
- #include <iostream>using namespace std;class Demo{public: Demo(); ~Demo();};Demo::Demo(){ cout<<"Constructor"<<endl;}Demo::~Demo(){ cout<<"Destructor"<<endl;}int main(){ cout<<"------new------"<<endl; Demo *p1 = new Demo; //创建一个对象 Demo *p2 = new Demo[5]; //创建一组对象 cout<<"------malloc------"<<endl<<endl; Demo *p3 = (Demo*)malloc(sizeof(Demo)); cout<<"------delete------"<<endl; delete p1; //销毁一个对象 delete[] p2; //销毁一组对象 cout<<"------free------"<<endl; free(p3); return 0;}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。