Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185507
  • 博文数量: 90
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2017-08-23 16:48
文章分类

全部博文(90)

文章存档

2015年(1)

2011年(21)

2010年(59)

2009年(9)

我的朋友
c++

分类: C/C++

2010-04-20 22:01:17


#include "stdafx.h"
#include
#include
using namespace std;
templateclass Pwarp
{
   T* ptr;
public:
 class RangeError{};
 Pwarp()
 {
  ptr = new T[sz];
  cout<< "Pwarp constructor!"< }
 ~Pwarp()
 {
  delete[]ptr;
  cout << " Pwarp destuctor" << endl;
 }
 T& operator[](int i)throw(RangeError)
 {
  if (i >= 0 && i < sz)
  {
   return ptr[i];
  }
  throw RangeError();
 }
};

class Cat
{
public:
 Cat(){cout << "Cat()" << endl; }
 ~Cat(){cout << "~Cat()"<< endl; }
 void g(){};
};
class Dog
{
public:
 void* operator new[](size_t){
  cout << "Allocating a dog!"<  throw 47;
 }
 void operator delete[](void *p){
  cout << "Deallocating a Dog"<< endl;
  ::operator delete[](p);
 }
};
class UseResources{
 PwarpCats;
 Pwarpdog;
public:
 UseResources(){cout << "UseResources()" << endl; }
 ~UseResources(){cout << "~UseResources()"<< endl ;}
 void f(){Cats[1].g();}
};

int _tmain(int argc, _TCHAR* argv[])
{
 try
 {
       UseResources us;
 }
 catch (int)
 {
       cout <<  "inside handler" << endl;
 }
 catch(...)
 {
  cout << "inside catch(...)"<< endl;
 }
 return 0;
}
阅读(885) | 评论(0) | 转发(0) |
0

上一篇:atuo_ptr 实现 csdn

下一篇:二分原理

给主人留下些什么吧!~~