I would like to ensure that an object’s destructor does not run. Is there any way to do this other than putting the object on the heap and not calling delete?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Not recommended, but here’s another way:
However, although this means the compiler won’t automatically call the destructor, the programmer still can:
Of course, that’s also true with heap allocation:
To prevent that, you’ll have to make the destructor inaccessible:
Or really inaccessible (related: Why must a base class destructor be accessible only when a custom constructor is declared?):