码迷,mamicode.com
首页 > 其他好文 > 详细

多态和异常处理的完美结合

时间:2017-05-07 22:05:15      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:size   null   cte   pre   protect   highlight   blog   异常处理   main   

#include<iostream>
using namespace std;

class MyArray
{
public:
	MyArray(int m_len);
	~MyArray();
public:
	class eSize
	{
	public:
		eSize(int len)
		{
			m_len11 = len;
		}
		virtual void printErr()
		{
			;
		}
	protected:
		int m_len11;
	};
	class eNegative :public eSize
	{
	public:
		eNegative(int len):eSize(len)
		{
			m_len11 = len;
		}
		virtual void printErr()
		{
			;
		}
	};
	class eZero:public eSize
	{
	public:
		eZero(int len):eSize(len)
		{
			m_len11 = len;
		}
		virtual void printErr()
		{
			;
		}
	};
	class eTooBig :public eSize
	{
	public:
		eTooBig(int len) :eSize(len)
		{
			m_len11 = len;
		}
		virtual void printErr()
		{
			;
		}

	};
	class eTooSmall :public eSize
	{
	public:
		eTooSmall(int len) :eSize(len)
		{
			m_len11 = len;
		}
		virtual void printErr()
		{
			;
		}

	};
protected:
private:
	int m_len;
	int *MySpace;

};

MyArray::MyArray(int m_len)
{
	this->m_len = m_len;
	
	if (m_len == 0)
		throw eZero(m_len);
	if (m_len <= 0)
		throw eNegative(m_len);
	if (m_len >= 1000)
		throw eTooBig(m_len);
	if (m_len <= 10)
		throw eTooSmall(m_len);
}
MyArray:: ~MyArray()
{
	if (MySpace != NULL)
	{
		delete [] MySpace;
		MySpace = NULL;
		m_len = 0;
	}
}
int main()
{
	try
	{
		MyArray a(-5);
		
	}
	catch (MyArray::eSize &e)
	{
		//cout <<  "len的大小: " << e.eSize();
		e.printErr();
	}
	catch (...)
	{
	}
		cout << "hello..." << endl;
	system("pause");
	return 0;
}

  

多态和异常处理的完美结合

标签:size   null   cte   pre   protect   highlight   blog   异常处理   main   

原文地址:http://www.cnblogs.com/xiaochige/p/6822312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!