"delete this" in C++ Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 45 Likes Like Report Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered.1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined. CPP class A { public: void fun() { delete this; } }; int main() { /* Following is Valid */ A *ptr = new A; ptr->fun(); ptr = NULL; // make ptr NULL to make sure that things are not accessed using ptr. /* And following is Invalid: Undefined Behavior */ A a; a.fun(); getchar(); return 0; } 2) Once delete this is done, any member of the deleted object should not be accessed after deletion. CPP #include<iostream> using namespace std; class A { int x; public: A() { x = 0;} void fun() { delete this; /* Invalid: Undefined Behavior */ cout<<x; // this is working } }; int main() { A* obj = new A; obj->fun(); return 0; } Output0 The best thing is to not do delete this at all.Thanks to Shekhu for providing above details.References: https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP05-CPP.+Avoid+deleting+this https://en.wikipedia.org/wiki/This_%28computer_science%29 Create Quiz Comment K kartik 45 Improve K kartik 45 Improve Article Tags : C++ secure-coding cpp-pointer Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like