C++异常what()函数重写
2016-04-20 23:50
281 查看
main里面这样:
for(;;){
try{
q_mytype.enQ(MyType());
}catch(exception& e){
cout<<"EXCEPTION!"<<e.what()<<endl;
break;
}
}
Queue.h里面这样:
template<typename T>
void Queue<T>::ensureCapacity(){
T* doubled;
if(size == capacity){ // The queue is full, and the user attempts to insert a new element.
try{
doubled = new T[capacity*2]; // Double the capacity.
}
catch(std::bad_alloc& e){ // When memory is not sufficient, it will throw an exception of bad_alloc.
throw NoSpaceForCapacity(); // Exception propagation.
}
std::cout<<"Capacity is doubled from "<<capacity<<" to "<<capacity*2<<std::endl;
T* old = Elements;
Elements = doubled;
for(int i = 0; i<capacity; i++){
Elements[i] = old[(start+i)%capacity]; // Copy elements from the old queue into the new one. Notice that the start position will be rewound to 0.
}
start = 0 ;
capacity*=2; // Update the information of capacity.
delete [] old;
}else{
return;
}
}
template<typename T>
void Queue<T>::enQ(T t){
ensureCapacity();
Elements[(start+size)%capacity] = t;
size++; // Update size.
return;
}
Exception.h里面这样:
class NoSpaceForCapacity: public std::bad_alloc{ // Memory is not large enough for the capacity of a queue to be doubled.
public:
NoSpaceForCapacity():bad_alloc(){
}
const char* what() const throw(){
return "No space for a doubled capacity!";
}
};
粗体字如果不写的话,main里面出来的字符串会是std::bad_alloc!
for(;;){
try{
q_mytype.enQ(MyType());
}catch(exception& e){
cout<<"EXCEPTION!"<<e.what()<<endl;
break;
}
}
Queue.h里面这样:
template<typename T>
void Queue<T>::ensureCapacity(){
T* doubled;
if(size == capacity){ // The queue is full, and the user attempts to insert a new element.
try{
doubled = new T[capacity*2]; // Double the capacity.
}
catch(std::bad_alloc& e){ // When memory is not sufficient, it will throw an exception of bad_alloc.
throw NoSpaceForCapacity(); // Exception propagation.
}
std::cout<<"Capacity is doubled from "<<capacity<<" to "<<capacity*2<<std::endl;
T* old = Elements;
Elements = doubled;
for(int i = 0; i<capacity; i++){
Elements[i] = old[(start+i)%capacity]; // Copy elements from the old queue into the new one. Notice that the start position will be rewound to 0.
}
start = 0 ;
capacity*=2; // Update the information of capacity.
delete [] old;
}else{
return;
}
}
template<typename T>
void Queue<T>::enQ(T t){
ensureCapacity();
Elements[(start+size)%capacity] = t;
size++; // Update size.
return;
}
Exception.h里面这样:
class NoSpaceForCapacity: public std::bad_alloc{ // Memory is not large enough for the capacity of a queue to be doubled.
public:
NoSpaceForCapacity():bad_alloc(){
}
const char* what() const throw(){
return "No space for a doubled capacity!";
}
};
粗体字如果不写的话,main里面出来的字符串会是std::bad_alloc!
相关文章推荐
- C++第4次实验剩余项目选做(非实验博客和作业博客)
- leetcode笔记:Maximal Square
- c++中的 extern "C"
- C++函数对象-谓词
- 经典排序算法之快速排序(C语言版)
- 一道笔试题:字符串变形
- C++语言中的引用的使用方法
- 使用OC调用C语言的函数
- C++/ C循环语句的效率问题
- c++第四次上机-6
- Effective C++ -- Item 1: View c++ as a federation of languages
- 南京理工大学第八届程序设计大赛(校外镜像) C count_prime
- VSVC2010中常用的C++11特性
- VSVC2010中常用的C++11特性
- C 语言 特点
- c++模板类和模板函数的使用
- VC++判断文件或文件夹是否存在
- C语言初级的小程序
- C++作业4
- 第四周 时间