析构函数
来源:互联网 发布:网络推广宣传单 编辑:程序博客网 时间:2024/04/29 08:30
析构函数
作用:释放内存
析构函数的名字与类名称是一样的,不同的就是在名字前面加(~),在程序中会被自动调用。
class X
{
public:
~p();
};
用构造函数和析构函数实现一个程序:
#include<iostream>
using namespace std;
class tree
{
public:
tree (int d);
~tree ();
void grow(int years);
void print();
private:
int height;
};
tree ::tree(int d)
{
height = d;
}
tree ::~tree()
{
cout << "clear"<<endl;
print();
}
void tree :: grow(int years)
{
height +=years;
}
void tree :: print()
{
cout << "the height is :"<<height<< endl;
}
int main()
{
tree T(10);
T.print();
T.grow(2);
}
注:在执行T.grow时,析构函数被自动调用对内存块进行清除,从下面的运行结果可以充分体现。
1 0
- 函数 - 构造函数 - 析构函数
- 构造函数、析构函数、虚函数
- 虚函数/构造函数/析构函数
- 构造函数&析构函数&虚函数
- 构造函数,析构函数
- 构造函数,析构函数
- 构造函数+析构函数
- 构造函数/析构函数
- 析构函数 虚函数
- 析构函数 虚函数
- 构造函数 & 析构函数
- 构造函数 析构函数
- 析构函数,构造函数
- 构造函数、析构函数
- 构造函数,析构函数
- 构造函数,析构函数
- 构造函数,析构函数
- 虚函数 析构函数
- 32.Your database instance is running with full workload after database creation. You have decided to
- 33.Some non-DBA users in your database have been granted ANY TABLE system privileges and they
- Java中JDBC的数据库连接池
- [Network Security]Anti-Debug Methods
- 了解流程类型,第 5 部分:原生广告 A/B 测试
- 析构函数
- PHP实现HTML页面静态化的方法
- 【7008】一笔画问题
- 设计模式(二) 工厂方法模式
- Java通信知识点汇集
- Java中IO流常见的几种流
- LIBSVM的使用方法与参数设置
- 线程和服务的比较
- 排序命令 - sort