体会template (转)

体会template (转)[@more@]

今天在我编程史上写下了光辉的一页:成功的使用模板编程,不但方便灵活,而且使代码精简了一半。爽!

要求:在图像上画手画线或不规则多边形,然后计算它们的周长与面积,再把周长、面积写到图像上
以前的代码是编写两个函数,因为操作的对象不同,这里分别为:LAnnFreehand,LAnnPolyline,他们都是继承于LAnnotatiion
代码如下:
/*
*函数名称: CalcEllipseArea
*函数功能: 计算椭圆面积
*函数参数1: &LAnn指定的注释类
*函数参数2:uAnnEvent响应事件类型
*返 回 值: 返回计算出的不规则模板的周长与面积字符串
*说  明: 为了适应不同的不规则注释,该函数使用泛型编程方法
*  使用模板定义所注释的类,可以大大节省代码空间
*/
template
CString CAnnDemoBitmap::CalcAbnoRmityTemp(T &LAnn,L_UINT uAnnEvent )
{
 int nPointCount;
 LBuffer Leadbuffer ; 
 pANNPOINT pPoints = NULL; file://对象点的指针
 int nRet; file://返回值
 int nGirth;
 int nArea;
 CString strMsg; file://annotation name

 if (uAnnEvent == LTANNEVENT_AUTOITEMCHANGED)
 {
 nPointCount = LAnn->GetPointCount();
 }
 else
 {
 nPointCount = LAnn->GetPointCount() + 1;
 }

 LeadBuffer.Reallocate( sizeof( ANNPOINT ) * nPointCount );
 pPoints = ( pANNPOINT )LeadBuffer.Lock() ;
 file://Now, get the points 
 nRet=LAnn->GetPoints( pPoints );
 pPoints[ nPointCount - 1 ] = pPoints[ 0 ];
 nRet=LAnn->SetPoints( pPoints, nPointCount );
 nGirth = CalcHandLineGirth( nPointCount, pPoints );
 nArea  = CalcHandLineArea( nPointCount, pPoints );
 strMsg.Format( "Girth:%dpixel Area:%d pixel", nGirth, nArea );
 LeadBuffer.Unlock();
 LeadBuffer.Free();

 return strMsg;
}
哇,原来使用泛型编程是如此之爽!


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-979263/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-979263/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值