什么时候我需要声明自己的析构函数?
如果类
不存在用户定义的析构函数而需要一个析构函数,则编译器会隐式声明一个析构函数。什么时候我需要声明自己的析构函数?
If no user-defined destructor exists for a class
and one is needed, the compiler implicitly declare a destructor. When I need to declare my own destructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当隐式声明的析构函数无法执行您需要的操作时。
这有点涉及。您应该查找并研究三法则。
When the implicitly declared destructor won't do what you need it to.
This is somewhat involved. You should look up and research the rule of three.
如果您分配内存、创建对象或执行任何其他操作,无论是在构造函数中还是之后,都需要在对象被析构时进行清理。
If you allocate memory, create objects, or anything else you do, either in the constructor or afterwards, that needs to be cleaned up when your object is destructed.
当你想确保某件事完成时。 “某事”通常会关闭网络连接或文件或释放一些内存等......
When you want to ensure something is finished. The 'something' would usually be closing a network connection or file or freeing up some memory etc...
每次你必须在对象销毁时执行特殊任务,即:内存释放、关闭网络连接、递减计数引用、线程同步、抛出存储的异常等。
Each time you must execute special tasks on object destruction, i.e: memory deallocation, close network connections, decrement count references, threads synchronization, throw stored exceptions, etc.