7

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?

EDIT

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

  1. 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++.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153

3 Answers3

7

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.

101010
  • 41,839
  • 11
  • 94
  • 168
2

There's no difference between these two lines of code. () is the same as (void). From a style perspective, () is what I've always seen for functions with no argument.

Almo
  • 15,538
  • 13
  • 67
  • 95
2

Both lines are equivalent. Some developers like to explicit (void) in signature of parameterless functions to say to anyone reading it "It does not take any parameters"

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
quantdev
  • 23,517
  • 5
  • 55
  • 88