手机就可以直接做设计的网站,南京it培训机构,wordpress影音,刷手机网站关键词析构函数 当类的对象撤销时#xff0c;析构函数被隐式调用。析构函数不是释放内存#xff0c;而是释放内存前进行扫尾工作。 对象何时撤销#xff1f;1#xff0c;静态分配的#xff0c;生存期过后撤销。2#xff0c;动态分配的#xff0c;delete时撤销。 析构函数的命名… 析构函数 当类的对象撤销时析构函数被隐式调用。析构函数不是释放内存而是释放内存前进行扫尾工作。 对象何时撤销1静态分配的生存期过后撤销。2动态分配的delete时撤销。 析构函数的命名 ~类型( ),析构函数没有形参和返回值。 一个类只能有一个析构函数如果程序员不显示的提供析构函数编译器提供默认的析构函数。 为包含动态分配的内存的类和使用系统资源的类构造适合的析构函数。 动态内存管理 //Cat.h
#includestring
#includeiostream
class Cat {
private:std::string name;int age;
public:Cat() {name huahua;age 0;}Cat(const std::string name,int age) {this-name name;this-age age;}~Cat() {std::cout name 析构...\n;}
}; #includestring
#includecstdlib
#includeCat.h
using namespace std;
int main() {double *ptr new double(3.14);delete ptr;//释放单个变量,deleteptr nullptr;//必须置为nullptrint *arr new int[10]();//()的意思默认初始化基本数据类型初始化为0bool初始化false//指针nullptr对象调用默认构造函数delete[] arr;//释放数组delete[]arr nullptr;Cat *c1 new Cat(mimi, 1);delete c1;Cat *catArr new Cat[3]();delete[] catArr;catArr nullptr;system(pause);return 0;
} mimi析构...huahua析构...huahua析构...huahua析构...请按任意键继续. . . 转载于:https://www.cnblogs.com/afreeman/p/8464501.html