删除指针不置空到底会发生什么后果

</pre><pre name="code" class="cpp"><textarea readonly="readonly" name="code" class="c++">
#include <iostream> 
using namespace std;

class Human
{
public:
	virtual void run() = 0;
	virtual void walk() = 0;
};

class Father : public Human
{
public:
	void run()
	{
		cout << "父亲能跑两万米" << endl;
		return;
	}
	void walk()
	{
		cout << "父亲能跳4米" << endl;
		return;
	}
};

class Mother : public Human
{
public:
	void walk()
	{
		cout << "母亲能跳2米" << endl;
		return;
	}
	void run()
	{
		cout << "母亲能跑1万米" << endl;
		return;
	}
};

class Son : public Father, public Mother
{ 
public:
	void run()
	{
		cout << "儿子是个少年" << endl;
	}
	void add()
	{
		cout << "儿子能跳3米跑3万米 " << endl;
		return;
	}

};

int main(void)
{
	Mother s1;
	Father s2;
	Son s3;

	Human * q = new Father;
	Human &p = s1;

	q->run();

	p.run();
	p.walk();

	Father * r = new Son;
	
	r->run();

	delete q;
	q = NULL;
	delete r;
	r = NULL;

	if (NULL != q)
		//r->walk();
		cout << "指针q未删除\n";
	if (NULL != r)
		//q->run();
		cout << "指针r未删除\n";

	Human * q1 = new Mother;
	q1->run();
	
	return 0;
}</textarea>


猜你喜欢

转载自blog.csdn.net/lxb18711871497/article/details/40383127