Looking at some old code of mine, I see that out of clumsiness I defined a destructor like so :
~ResourceManager(void);
This not only compiles, but works as expected. I changed it of course to
~ResourceManager();
but was I too quick to refactor? Is the first version correct and good C++ style?
Since the question was closed and won't get any chance for proper disambiguation, I should put the related quote from the standard that answers this question, when destructors are put in perspective
12.4 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. In such a declaration, the ˜ followed by the destructor’s class name can be enclosed in optional parentheses; such parentheses are ignored. A typedef-name shall not be used as the class-name following the ∼ in the declarator for a destructor declaration.
So the standard mandates an empty parameter list. Maybe backwards compatibility with C practices for free functions (where f(void)
is the way to declare an empty parameter list) took destructors along with them in implementations, but it certainly doesn't seem to be valid C++.
Destructor function is automatically invoked when the objects are destroyed. It cannot be declared static or const. The destructor does not have arguments. It has no return type not even void.
A destructor automatically initiates. So, you can not make more destructors than 1.
C++ destructors are called twice for local objects when exiting a function via a nested return statement.
A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const , volatile , const volatile or static . A destructor can be declared virtual or pure virtual .
This is a remnant from C. In C the empty parameter list does not mean a function that takes no arguments, but rather a function with unspecified number of arguments. In C++ foo(void)
is deprecated, and foo()
is preferred, as it specifically means a function that takes no parameters. The same goes with your destructor as well. Although, the two lines of code are equivalent you should prefer the ~ResourceManager()
version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With