Reputation: 7425
In a class (without direct pointer members), I see there are following 3 possibilities for defining a destructor.
class Child : public Parent
{
public:
// ~Child() override {} // (1) explicit destructor with empty body
// ~Child() override = default; // (2) explicit default destructor
// // (3) implicit default destructor
private:
// members
}
Can/should option (1)
always be avoided? Because Clang-Tidy hints me to take option (2)
if I use option (1)
.
What are the differences between the three different options in general? What should be considered when selecting one over others?
Upvotes: 4
Views: 2129
Reputation: 238311
Can/should option (1) always be avoided?
Assuming non-ancient version of the language, yes. As far as I can tell, only reason to use an empty non-default destructor is to support C++03 and older standard.
What should be considered when selecting one over others?
override
specifier).As a rough rule of thumb, use 3. if possible. If not possible (for example, the PIMPL case described above), then use 2. If not possible (i.e. you need to support C++03), then use 1.
Upvotes: 5
Reputation: 75688
C++ uses RAII principle. Closely related there is The rule of three/five/zero.
It goes like this:
You are definitely in the Rule of Zero territory as should be all of your code. So you need to have the implicit destructor.
Upvotes: 3
Reputation: 27567
Can/should option (1) always be avoided?
If you have nothing to put into a destructor, then you should let the compiler generate a default
destructor for you, so Yes.
What are the differences between the three different options in general?
Assuming that there's nothing special that needs to be put in your destructor:
You lock the destrucor into being empty, if things change you might forget to add them into your destructor.
Let the compiler figure it all out and show in the code that your doing this.
Let the compiler figure it all out and don't show in the code that your doing this.
Upvotes: 3