Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to end a destructor with a return statement?

Tags:

c++

destructor

In my doubly-linked list class, I am coding my destructor and this is my code:

DLinkedList::~DLinkedList() {
    if (head==NULL) {
        return;
    }

    // Other code
}

Is it safe to end a destructor with a return; statement?

I know that I can end my void functions with a return; statement, but this is a destructor.

like image 237
levidone Avatar asked Jan 01 '19 13:01

levidone


People also ask

Can you return in destructor?

Properties of Destructor: The destructor does not have arguments. It has no return type not even void.

Is destructor called after return?

However, in your current implementation you're actually returning a shallow copy of NodeContainer. Once your copy goes out of scope its destructor is called, which deallocates its memory, which in this case is the original memory of your member, effectively making your member invalid.

Is destructor called before return?

While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function.

How do you stop a destructor in C++?

The best way to not call a particular destructor is to not create an instance of that object to begin with. Failing that, take the code you don't want to run out of the destructor.


3 Answers

Is it safe to end a destructor with return; statement? I know that I can end my void functions with a return; statement, but this is a destructor.

There's not much difference of a destructor function from a function with void return type, besides the destructor function is executed automatically1 whenever the class's lifetime ends.

You use return; if the execution of the destructor function should be stopped, as you do with any other function.


1)The same applies for constructor functions BTW.

like image 178
πάντα ῥεῖ Avatar answered Sep 18 '22 20:09

πάντα ῥεῖ


Yes.

In this sense, the destructor body acts much like a function that returns void, except that the bases and members will still be destroyed even if you return early (since this never relied on the contents of the destructor body anyway).

Observe the following rules:

[special]/1: The default constructor ([class.default.ctor]), copy constructor, move constructor ([class.copy.ctor]), copy assignment operator, move assignment operator ([class.copy.assign]), and destructor ([class.dtor]) are special member functions. [..]

[stmt.return]/1: A function returns to its caller by the return statement.

[stmt.return]/2: The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used only in a function whose return type is cv void, a constructor, or a destructor. [..]

[class.dtor]/9: [..] A return statement ([stmt.return]) in a destructor might not directly return to the caller; before transferring control to the caller, the destructors for the members and bases are called. [..]

like image 45
Lightness Races in Orbit Avatar answered Sep 16 '22 20:09

Lightness Races in Orbit


Yes, it's OK to end the execution of a destructor with a return.

like image 43
gsamaras Avatar answered Sep 16 '22 20:09

gsamaras