删除函数后,为什么仍然可以从指针类中调用函数? [英] Why can I still call a function from a pointer class after I delete it?
本文介绍了删除函数后,为什么仍然可以从指针类中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Possible Duplicate:
What will happen when I call a member function on a NULL object pointer?
#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;
}
::: OUTPUT :::
:::OUTPUT:::
结果:2
结果:2
结果:2
我只是在用C ++弄乱,试图了解更多信息.我想看看删除操作符到底是做什么的.为什么删除对象后仍然得到"result:2"的第三个输出?
I am just messing around in C++ trying to learn more. I wanted to see what exactly the delete operator does. Why is it that I still get a third output of "result: 2" after deleting the object?
推荐答案
对象内存可能尚未被其他内容覆盖.不要做这种事情,这是未定义的行为
The object memory probably was not overwritten by something else yet. Do not do such things, it is undefined behavior
这篇关于删除函数后,为什么仍然可以从指针类中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文