《Effective C++》笔记(一)

1,处理operator new内存分配失败的情况
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

None.gif#include<iostream>
None.gif
usingnamespacestd;
None.giftemplate
<typenameT>
None.gif
classnewhandlersupport
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{//提供类set_new_handler支持的基类
InBlock.gif
public:
InBlock.gif
staticnew_handlerset_new_handler(new_handlerp);
InBlock.gif
staticvoid*operatornew(size_tsize);//重载operatornew运算符
InBlock.gif
private:
InBlock.gif
staticnew_handlercurrenthandler;//当前的new_handler
ExpandedBlockEnd.gif
}
;
None.giftemplate
<typenameT>
None.gifnew_handlernewhandlersupport
<T>::set_new_handler(new_handlerp)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifnew_handleroldhandler
=currenthandler;
InBlock.gifcurrenthandler
=p;
InBlock.gif
returnoldhandler;
ExpandedBlockEnd.gif}

None.giftemplate
<typenameT>
None.gif
void*newhandlersupport<T>::operatornew(size_tsize)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifnew_handlerglobalhandler
=std::set_new_handler(currenthandler);//申请内存前先设置新的new_Handler
InBlock.gif
void*memory;
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmemory
=::operatornew(size);//实际分配内存
ExpandedSubBlockEnd.gif
}

InBlock.gif
catch(std::bad_alloc&)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstd::set_new_handler(globalhandler);
//恢复旧的new_handler
InBlock.gif
throw;//抛出异常
ExpandedSubBlockEnd.gif
}

InBlock.gifstd::set_new_handler(globalhandler);
InBlock.gif
returnmemory;
ExpandedBlockEnd.gif}

None.giftemplate
<typenameT>
None.gifnew_handlernewhandlersupport
<T>::currenthandler=NULL;
None.gif
None.gif
None.gif
classGamePlayer:publicnewhandlersupport<GamePlayer>
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
private:
InBlock.gif
staticconstintNUM_TURNS;
InBlock.gif
int*scores;
InBlock.gif
InBlock.gif
public:
InBlock.gifGamePlayer();
InBlock.gif
~GamePlayer();
ExpandedBlockEnd.gif}
;
None.gif
constintGamePlayer::NUM_TURNS=5;
None.gifGamePlayer::GamePlayer()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifcout
<<"构造函数"<<endl;
InBlock.gifscores
=newint[NUM_TURNS];
InBlock.gifmemset(scores,
0,sizeof(scores));
InBlock.gif
for(inti=0;i<NUM_TURNS;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifscores[i]
=i;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gifGamePlayer::
~GamePlayer()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifcout
<<"析构函数"<<endl;
InBlock.gif
for(inti=0;i<NUM_TURNS;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<scores[i]<<'/t';
ExpandedSubBlockEnd.gif}

InBlock.gifcout
<<endl;
InBlock.gifdelete[]scores;
InBlock.gifscores
=NULL;
ExpandedBlockEnd.gif}

None.gif
voidnomorememory()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifcerr
<<"unabletosatisfyrequestformemory/n";
InBlock.gifabort();
ExpandedBlockEnd.gif}

None.gif
intmain()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif::set_new_handler(nomorememory);
InBlock.gifGamePlayer::set_new_handler(nomorememory);
InBlock.gifGamePlayer
*pPlayOne=newGamePlayer();
InBlock.gifdeletepPlayOne;
InBlock.gifGamePlayer::set_new_handler(NULL);
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifGamePlayer
*pPlayTwo=newGamePlayer();
InBlock.gifdeletepPlayTwo;
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(std::bad_alloc&)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcerr
<<"operatornewfailed"<<endl;
ExpandedSubBlockEnd.gif}

InBlock.gifsystem(
"pause");
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值