为什么输出为221求解😳

#include

using namespace std;

class Obj{

 static int i;

public:

 Obj(){i++;}

 ~Obj(){i--;}

 static int getVal(){return i;}

};

int Obj::i=0;

void f()

{

 Obj ob2;

 cout<<ob2.getVal();

}

int main()

{

 Obj ob1;

 f();

 Obj*ob3=new Obj;

 cout<<ob3->getVal();

 delete ob3;

 cout<<Obj::getVal();

 return 0;

}

运行时的输出结果是( )。

执行顺序如下
obj obj ;//执行构造函数 Obj(){i++;},执行过之后i=1;
然后执行f()函数,obj obj2; 执行i++此时 Obj(){i++;} i=2,然后执行 static int getVal(){return i;} , cout<<ob2.getVal();等于2 执行完毕后,执行 Obj(){i--;} 此时i=1; 再执行 Obj*ob3=new Obj; 又执行了一次Obj(){i++;}i等于2,delete之后执行了obj() i=1,所以结果是221

根据我写的代码理解一下

#include<stdio.h>
#include<iostream>
#include<cstdio>
#include <algorithm>
using namespace std;

class Obj{
 static int i;
public:

 Obj(){i++;cout<<endl<<"这是构造函数显示的i="<<i<<endl;}
 
 ~Obj(){i--;cout<<endl<<"这是析构函数显示的i="<<i<<endl;}
 
 static int getVal(){return i;}
};

int Obj::i=0;

void f()

{
 Obj ob2;
 
 cout<<endl<<"ob2.getVal()这是f函数显示的(原来输出的2)i="<<ob2.getVal()<<endl;;
}

int main()

{
 Obj ob1;    

 f();
 
 Obj*ob3=new Obj;
 
 cout<<endl<<"这是ob3->getVal()(原来输出的2),i="<<ob3->getVal()<<endl;
 
 delete ob3;
 
 cout<<endl<<"这是Obj::getVal()(原来输出的1),i="<<Obj::getVal()<<endl;
 
 return 0;
}

显示结果:

这是构造函数显示的i=1

这是构造函数显示的i=2

ob2.getVal()这是f函数显示的(原来输出的2)i=2

这是析构函数显示的i=1

这是构造函数显示的i=2

这是ob3->getVal()(原来输出的2),i=2

这是析构函数显示的i=1

这是Obj::getVal()(原来输出的1),i=1

这是析构函数显示的i=0


Process exited after 0.5204 seconds with return value 0
请按任意键继续. . .

单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。