• Tutorials
  • Courses
  • Tracks

C++ | this pointer | Question 5

Last Updated :
Discuss
Comments

Predict the output of following C++ program?

CPP
#include<iostream>
using namespace std;

class Test
{
private:
  int x;
public:
  Test() {x = 0;}
  void destroy()  { delete this; }
  void print() { cout << "x = " << x; }
};

int main()
{
  Test obj;
  obj.destroy();
  obj.print();
  return 0;
}

x = 0

undefined behavior

compiler error

runtime error

Share your thoughts in the comments