#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;
}
阅读(927) | 评论(0) | 转发(0) |