Destructor parameters

2019-04-03 03:47发布

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.

3条回答
走好不送
2楼-- · 2019-04-03 04:06

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?

查看更多
聊天终结者
3楼-- · 2019-04-03 04:12

Section §12.4 of C++0x draft n3290 has this to say about destructors:

Destructors

A special declarator syntax using an optional function-specifier (7.1.2) followed by ˜ followed by the destructor’s class name followed by an empty parameter list is used to declare the destructor in a class definition.

(emphasis added)

So no, destructors do not take parameters. (The 2003 standard has the exact wording of the above paragraph.)

查看更多
Deceive 欺骗
4楼-- · 2019-04-03 04:14

No. You hardly ever call them directly anyway, so what would be the use.

The destructor is supposed to destroy the object, nothing more.

查看更多
登录 后发表回答