#include <iostream>
#include <string>
using namespace std;
class myClass{
private:
int *x, *y, *z;
public:
myClass();
~myClass();
void display();
void math(int,int,int);
};
void myClass::math(int x,int y,int z){
this->x = new int;
this->y = new int;
this->z = new int;
*this->x = x;
*this->y = y;
*this->z = z;
cout << "result: " << (x*y)+z << endl;
}
myClass::~myClass(){
delete x;
delete y;
delete z;
}
void myClass::display(){
cout << x << y << z << endl;
}
myClass::myClass(){
x=0;
y=0;
z=0;
}
int main()
{
myClass myclass;
myClass *myptr;
myptr = new myClass();
myclass.math(1,1,1);
myptr->math(1,1,1);
delete myptr;
myptr->math(1,1,1); **//why does this still print?**
int t;
cin >> t;
}
:::输出:::
结果:2
结果:2
结果:2
我只是在 C++ 中胡闹,试图了解更多。我想看看 delete 操作符到底做了什么。为什么删除对象后我仍然得到“结果:2”的第三个输出?