设为首页 加入收藏

TOP

1.C++异常处理(二)
2015-07-20 17:44:43 来源: 作者: 【 】 浏览:7
Tags:1.C 异常 处理

\

6.模板类的异常

#include

usingnamespacestd;

//typename会明确类型

//模板的异常,处理通用的数据类型,类中包含一个如果使用

//虚函数,虚函数可以指针,引用来实现

//异常处理机制,一个接口处理通用的异常

template

classArray

{

public:

classwrong

{

public:

//虚函数

virtualvoidshow()

{

cout <<" wrong " <

}

};

classbig :publicwrong

{

public:

intx;

big(intn) :x(n){}

voidshow()

{

cout <<"big wrong"<

}

};

classsmall :publicwrong

{

public:

intx;

small(intn) :x(n){}

voidshow()

{

cout <<"small wrong" <

}

};

Array(intn)

{

if (n > 0 && n<10)

{

throwsmall(n);

}

elseif (n>10000)

{

throwbig(n);

}

elseif (n < 0)

{

throwwrong();

}

else

{

p =newT[n];

size =n;

}

}

private:

intsize;

T *p;

};

voidmain()

{

try

{

Array may(1);

}

catch (Array ::wrong & wrong1)

{

wrong1.show();

}

cin.get();

}

运行结果:

\

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 4777 Rabbit Kingdom (离线.. 下一篇topcoder srm 623解题报告

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)