【发布时间】:2017-08-14 09:39:59
【问题描述】:
“终止内务处理”是什么意思? 我读过析构函数用于对类的对象执行终止内务处理。不知道是什么意思。
谢谢。
【问题讨论】:
-
我知道什么是析构函数,我知道它是做什么的,我想知道我提到的表达式是什么意思。
-
“终止”通常是指程序的结束。
标签: c++ class destructor
“终止内务处理”是什么意思? 我读过析构函数用于对类的对象执行终止内务处理。不知道是什么意思。
谢谢。
【问题讨论】:
标签: c++ class destructor
在析构函数的上下文中,终止内务处理是在对象被销毁之前要完成的工作。
如果你想在系统回收对象的存储之前做一些事情,在析构函数中编写代码。
例如,初学者用它来理解被调用的构造函数和析构函数的顺序。
让我们以here为例:
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void) {
cout << "Object is being created" << endl;
}
Line::~Line(void) {
// THE PLACE FOR TERMINATION HOUSEKEEPING
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
int main( ) {
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
你可以看另一个例子here。
【讨论】: