现在的位置: 首页 > 综合 > 正文

析构函数终于惹祸了。。。。

2013年06月23日 ⁄ 综合 ⁄ 共 3189字 ⁄ 字号 评论关闭

先看我的代码:

1、定义类的头文件

#include <iostream>

#ifndef CHECKPTR_H
#define CHECKPTR_H
class CheckPtr
{
	//friend std::ostream& operator<<(std::ostream&, const CheckPtr&);
	friend const int& operator*(const CheckPtr&);
	friend int& operator*(CheckPtr&);
public:
	CheckPtr& operator++();
	const CheckPtr operator++(int);
	CheckPtr& operator--();
	const CheckPtr operator--(int);
	bool operator==(const CheckPtr&);
	bool operator!=(const CheckPtr&);
	bool operator<(const CheckPtr&);
	bool operator>(const CheckPtr&);
	bool operator<=(const CheckPtr&);
	bool operator>=(const CheckPtr&);
	const CheckPtr& operator+(const unsigned&);
	const CheckPtr& operator-(const unsigned&);
	CheckPtr& operator=(const CheckPtr&);
	CheckPtr& operator+=(const unsigned&);
	CheckPtr& operator-=(const unsigned&);
	CheckPtr(int *pb, int *pe): beg(pb), end(pe), cur(pb) {}
	CheckPtr(const CheckPtr &ptr): beg(ptr.beg), end(ptr.end), cur(ptr.cur) {}
	//这个析构函数惹祸了。。。。
	//~CheckPtr() { delete beg; delete end; delete cur; }
private:
	int *beg;
	int *end;
	int *cur;
};

#endif

2、定义类成员函数及友元函数的文件

#include "CheckPtr.h"
#include <iostream>

CheckPtr& CheckPtr::operator++()
{
	if (cur == end){
		//std::cout << "超出范围!" << std::endl;
		return *this;
	}
	++cur;
	return *this;
}

const CheckPtr CheckPtr::operator++(int)
{
	CheckPtr ret(*this);
	++*this;
	return ret;
}

CheckPtr& CheckPtr::operator--()
{
	if (cur == beg){
		//std::cout << "超出范围!" << std::endl;
		return *this;
	}
	--cur;
	return *this;
}

const CheckPtr CheckPtr::operator--(int)
{
	CheckPtr ret(*this);
	--*this;
	return ret;
}

bool CheckPtr::operator==(const CheckPtr &ptr)
{
	if (beg == ptr.beg && end == ptr.end && cur == ptr.cur)
		return true;
	else
		return false;
}

bool CheckPtr::operator!=(const CheckPtr &ptr)
{
	if (beg != ptr.beg || end != ptr.end || cur != ptr.cur)
		return true;
	else
		return false;
}

//对于不指向同一数组的,进行小于关系比较,始终返回假。
//其他关系比较也相同
bool CheckPtr::operator<(const CheckPtr &ptr)
{
	if (beg == ptr.beg && end == ptr.end)
	{
		if (cur < ptr.cur)
			return true;
		else
			return false;
	}
	else
		return false;
}

bool CheckPtr::operator>(const CheckPtr &ptr)
{
	if (beg == ptr.beg && end == ptr.end)
	{
		if (cur > ptr.cur)
			return true;
		else
			return false;
	}
	else
		return false;
}

bool CheckPtr::operator<=(const CheckPtr &ptr)
{
	if (beg == ptr.beg && end == ptr.end)
	{
		if (cur <= ptr.cur)
			return true;
		else
			return false;
	}
	else
		return false;
}

bool CheckPtr::operator>=(const CheckPtr &ptr)
{
	if (beg == ptr.beg && end == ptr.end)
	{
		if (cur >= ptr.cur)
			return true;
		else
			return false;
	}
	else
		return false;
}

const CheckPtr& CheckPtr::operator+(const unsigned &ix)
{
	if (cur + ix < end)
		cur += ix;
	return *this;
}

const CheckPtr& CheckPtr::operator-(const unsigned &ix)
{
	if (cur - ix >= beg)
		cur -= ix;
	return *this;
}

CheckPtr& CheckPtr::operator=(const CheckPtr &ptr)
{
	beg = ptr.beg;
	end = ptr.end;
	cur = ptr.cur;
	return *this;
}

CheckPtr& CheckPtr::operator+=(const unsigned &ix)
{
	if (cur + ix < end)
		cur += ix;
	return *this;
}

CheckPtr& CheckPtr::operator-=(const unsigned &ix)
{
	if (cur - ix >= beg)
		cur -= ix;
	return *this;
}

int& operator*(CheckPtr &ptr)
{	return *ptr.cur;   }

const int& operator*(const CheckPtr &ptr)
{ return *ptr.cur; }

/*
std::ostream& operator<<(std::ostream &os, const CheckPtr &ptr)
{
	os << *ptr.cur;
	return os;
}
*/

3、测试文件代码,main

#include "CheckPtr.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "请输入5个整数:" << endl;
	int arr[5];
	for (unsigned i = 0; i != 5; ++i)
		cin >> arr[i];
	CheckPtr arr_ptr(arr, arr + 4);
	cout << "你的CheckPtr对象的数字是:" << endl;
	for (unsigned num = 0; num != 5; ++num){
		cout << *arr_ptr++ << endl;
	}
	return 0;
}

在用了上述析构函数后,输入5个整数后,在进入for循环后,就显示不了。个人推想,因为我这个后置++返回的是临时变量,虽然我定义的后置++没用引用,但是在返回后,临时变量生命到期,调用了析构函数,把指针删除了,导致程序出错。

可是,问题是,我用前置++形式,也有问题。不知道这是为什么呢?

抱歉!评论已关闭.