Is it safe to end destructor with return statement?
In my Doubly Linked list class, I am coding my destructor and this is my code:
DLinkedList::~DLinkedList() {
if(head==NULL){
return;
}
//other code
}
My question is, is it safe to end a destructor with return;
statement? I know that I can use to end my void
functions with return;
statement, but this is a destructor.
c++ destructor
add a comment |
In my Doubly Linked list class, I am coding my destructor and this is my code:
DLinkedList::~DLinkedList() {
if(head==NULL){
return;
}
//other code
}
My question is, is it safe to end a destructor with return;
statement? I know that I can use to end my void
functions with return;
statement, but this is a destructor.
c++ destructor
4
Yes, it's no worse than ending avoid
function early. But depending on what the//other code
is, you might be able to rewrite your destructor so that theif
is not needed at all.
– HolyBlackCat
1 hour ago
5
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nestedif
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)
– Scheff
1 hour ago
2
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
1
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago
add a comment |
In my Doubly Linked list class, I am coding my destructor and this is my code:
DLinkedList::~DLinkedList() {
if(head==NULL){
return;
}
//other code
}
My question is, is it safe to end a destructor with return;
statement? I know that I can use to end my void
functions with return;
statement, but this is a destructor.
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
}
My question is, is it safe to end a destructor with return;
statement? I know that I can use to end my void
functions with return;
statement, but this is a destructor.
c++ destructor
c++ destructor
edited 1 hour ago
πάντα ῥεῖ
71.9k972134
71.9k972134
asked 1 hour ago
levidone
84110
84110
4
Yes, it's no worse than ending avoid
function early. But depending on what the//other code
is, you might be able to rewrite your destructor so that theif
is not needed at all.
– HolyBlackCat
1 hour ago
5
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nestedif
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)
– Scheff
1 hour ago
2
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
1
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago
add a comment |
4
Yes, it's no worse than ending avoid
function early. But depending on what the//other code
is, you might be able to rewrite your destructor so that theif
is not needed at all.
– HolyBlackCat
1 hour ago
5
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nestedif
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)
– Scheff
1 hour ago
2
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
1
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago
4
4
Yes, it's no worse than ending a
void
function early. But depending on what the //other code
is, you might be able to rewrite your destructor so that the if
is not needed at all.– HolyBlackCat
1 hour ago
Yes, it's no worse than ending a
void
function early. But depending on what the //other code
is, you might be able to rewrite your destructor so that the if
is not needed at all.– HolyBlackCat
1 hour ago
5
5
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nested
if
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)– Scheff
1 hour ago
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nested
if
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)– Scheff
1 hour ago
2
2
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
1
1
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago
add a comment |
2 Answers
2
active
oldest
votes
My question is, is it safe to end a destructor with
return;
statement? I know that I can use to end myvoid
functions withreturn;
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.
add a comment |
Yes, it's OK to end the execution of a destructor with a return
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995936%2fis-it-safe-to-end-destructor-with-return-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
My question is, is it safe to end a destructor with
return;
statement? I know that I can use to end myvoid
functions withreturn;
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.
add a comment |
My question is, is it safe to end a destructor with
return;
statement? I know that I can use to end myvoid
functions withreturn;
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.
add a comment |
My question is, is it safe to end a destructor with
return;
statement? I know that I can use to end myvoid
functions withreturn;
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.
My question is, is it safe to end a destructor with
return;
statement? I know that I can use to end myvoid
functions withreturn;
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.
edited 28 mins ago
answered 1 hour ago
πάντα ῥεῖ
71.9k972134
71.9k972134
add a comment |
add a comment |
Yes, it's OK to end the execution of a destructor with a return
.
add a comment |
Yes, it's OK to end the execution of a destructor with a return
.
add a comment |
Yes, it's OK to end the execution of a destructor with a return
.
Yes, it's OK to end the execution of a destructor with a return
.
answered 1 hour ago
gsamaras
50.5k2399186
50.5k2399186
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995936%2fis-it-safe-to-end-destructor-with-return-statement%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Yes, it's no worse than ending a
void
function early. But depending on what the//other code
is, you might be able to rewrite your destructor so that theif
is not needed at all.– HolyBlackCat
1 hour ago
5
It's OK for early out. However, I sometimes read that certain programmers consider early out as bad style. I personally think, cascades of nested
if
s are not that much better. So, it might be a matter of taste. I found a Q/A in SE about this: SE: Should I return from a function early or use an if statement? Consider that it has been closed due to primarily opinion-based... ;-)– Scheff
1 hour ago
2
Normative reference: eel.is/c++draft/stmt.return#2
– peppe
59 mins ago
The structured programming paradigm frowns on early return. For my C++ code, I return whenever it is appropriate, but there are lots of C++ folks who try hard to make their code only return at the very end. I presume they were taught that habit, and adhere to it. (Exceptions being an exception, I suppose, since they're another return path.) en.wikipedia.org/wiki/Structured_programming
– Eljay
34 mins ago
1
Let me answer your question with another question: Why would you think it might not be safe? Did you think that it might return 'more' than hitting the closing brace would, and thus skip something that would normally happen after the destructor body? If "safe" is just a misnomer for 'valid code that a compiler should accept', then you should just turn on warnings to know that.
– underscore_d
20 mins ago