《Effective C++》笔记(一)
1,处理operator new内存分配失败的情况
#include <iostream>
using namespace std;
template<typename T>
class newhandlersupport
{ //提供类set_new_handler支持的基类
public:
static new_handler set_new_handler(new_handler p);
static void* operator new(size_t size);//重载operator new运算符
private:
static new_handler currenthandler;//当前的new_handler
};
template<typename T>
new_handler newhandlersupport<T>::set_new_handler(new_handler p)
{
new_handler oldhandler = currenthandler;
currenthandler = p;
return oldhandler;
}
template<typename T>
void* newhandlersupport<T>::operator new(size_t size)
{
new_handler globalhandler = std::set_new_handler(currenthandler);//申请内存前先设置新的new_Handler
void *memory;
try
{
memory = ::operator new(size);//实际分配内存
}
catch (std::bad_alloc&)
{
std::set_new_handler(globalhandler);//恢复旧的new_handler
throw;//抛出异常
}
std::set_new_handler(globalhandler);
return memory;
}
template<typename T>
new_handler newhandlersupport<T>::currenthandler = NULL;
class GamePlayer : public newhandlersupport<GamePlayer>
{
private:
static const int NUM_TURNS;
int *scores;
public:
GamePlayer();
~GamePlayer();
};
const int GamePlayer::NUM_TURNS = 5;
GamePlayer::GamePlayer()
{
cout<<"构造函数"<<endl;
scores = new int[NUM_TURNS];
memset(scores,0,sizeof(scores));
for (int i =0;i<NUM_TURNS;++i)
{
scores[i] = i;
}
}
GamePlayer::~GamePlayer()
{
cout<<"析构函数"<<endl;
for (int i=0;i<NUM_TURNS;++i)
{
cout<<scores[i]<<'\t';
}
cout<<endl;
delete [] scores;
scores = NULL;
}
void nomorememory()
{
cerr << "unable to satisfy request for memory\n";
abort();
}
int main()
{
::set_new_handler(nomorememory);
GamePlayer::set_new_handler(nomorememory);
GamePlayer* pPlayOne = new GamePlayer();
delete pPlayOne;
GamePlayer::set_new_handler(NULL);
try
{
GamePlayer* pPlayTwo = new GamePlayer();
delete pPlayTwo;
}
catch (std::bad_alloc&)
{
cerr<<"operator new failed"<<endl;
}
system("pause");
return 0;
}
using namespace std;
template<typename T>
class newhandlersupport
{ //提供类set_new_handler支持的基类
public:
static new_handler set_new_handler(new_handler p);
static void* operator new(size_t size);//重载operator new运算符
private:
static new_handler currenthandler;//当前的new_handler
};
template<typename T>
new_handler newhandlersupport<T>::set_new_handler(new_handler p)
{
new_handler oldhandler = currenthandler;
currenthandler = p;
return oldhandler;
}
template<typename T>
void* newhandlersupport<T>::operator new(size_t size)
{
new_handler globalhandler = std::set_new_handler(currenthandler);//申请内存前先设置新的new_Handler
void *memory;
try
{
memory = ::operator new(size);//实际分配内存
}
catch (std::bad_alloc&)
{
std::set_new_handler(globalhandler);//恢复旧的new_handler
throw;//抛出异常
}
std::set_new_handler(globalhandler);
return memory;
}
template<typename T>
new_handler newhandlersupport<T>::currenthandler = NULL;
class GamePlayer : public newhandlersupport<GamePlayer>
{
private:
static const int NUM_TURNS;
int *scores;
public:
GamePlayer();
~GamePlayer();
};
const int GamePlayer::NUM_TURNS = 5;
GamePlayer::GamePlayer()
{
cout<<"构造函数"<<endl;
scores = new int[NUM_TURNS];
memset(scores,0,sizeof(scores));
for (int i =0;i<NUM_TURNS;++i)
{
scores[i] = i;
}
}
GamePlayer::~GamePlayer()
{
cout<<"析构函数"<<endl;
for (int i=0;i<NUM_TURNS;++i)
{
cout<<scores[i]<<'\t';
}
cout<<endl;
delete [] scores;
scores = NULL;
}
void nomorememory()
{
cerr << "unable to satisfy request for memory\n";
abort();
}
int main()
{
::set_new_handler(nomorememory);
GamePlayer::set_new_handler(nomorememory);
GamePlayer* pPlayOne = new GamePlayer();
delete pPlayOne;
GamePlayer::set_new_handler(NULL);
try
{
GamePlayer* pPlayTwo = new GamePlayer();
delete pPlayTwo;
}
catch (std::bad_alloc&)
{
cerr<<"operator new failed"<<endl;
}
system("pause");
return 0;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2008-04-15 13:25 Phinecos(洞庭散人) 阅读(657) 评论(3) 编辑 收藏 举报