C++ 析构函数参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6245295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Destructor parameters
提问by Thomas Matthews
The article Are destructors overloadable?talks about overloading the destructor.
文章析构函数是否可重载?谈论重载析构函数。
This raised a question: Can a destructor have parameters?
这就提出了一个问题:析构函数可以有参数吗?
I've never used or seen a destructor with parameters. I could not come up with an example of a reason to use parameters to the destructor.
我从未使用或见过带参数的析构函数。我想不出一个使用析构函数参数的例子。
回答by Mat
Section §12.4 of C++0x draft n3290 has this to say about destructors:
C++0x 草案 n3290 的第 12.4 节有关于析构函数的说明:
Destructors
A special declarator syntax using an optional function-speci?er (7.1.2) followed by ? followed by the destructor's class name followed by an empty parameter listis used to declare the destructor in a class de?nition.
析构函数
使用可选函数说明符 (7.1.2) 后跟 ? 后跟析构函数的类名后跟空参数列表用于在类定义中声明析构函数。
(emphasis added)
(强调)
So no, destructors do not take parameters. (The 2003 standard has the exact wording of the above paragraph.)
所以不,析构函数不接受参数。(2003 年标准具有上段的确切措辞。)
回答by Puppy
No, is the simple answer. This would make automatic resource management a significant bitch, because you'd have to worry about what parameters the destructor took and where the hell you were going to get them from. What about in the case of exception- how would the compiler know what to pass your destructor?
不,是简单的答案。这将使自动资源管理成为一个重要的婊子,因为您必须担心析构函数采用哪些参数以及您将从哪里获得它们。在异常的情况下呢 - 编译器如何知道传递析构函数的内容?
回答by Bo Persson
No. You hardly ever call them directly anyway, so what would be the use.
不。反正你几乎从来没有直接打电话给他们,所以有什么用。
The destructor is supposed to destroy the object, nothing more.
析构函数应该销毁对象,仅此而已。