Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a ~destructor(void)? [duplicate]

Tags:

c++

standards

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

like image 252
Nikos Athanasiou Avatar asked May 30 '14 14:05

Nikos Athanasiou


People also ask

Can destructor be void?

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.

Can we have more than one destructor?

A destructor automatically initiates. So, you can not make more destructors than 1.

Why is destructor called twice?

C++ destructors are called twice for local objects when exiting a function via a nested return statement.

How many arguments destructor can have?

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 .


1 Answers

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.

like image 147
101010 Avatar answered Oct 13 '22 19:10

101010