码迷,mamicode.com
首页 > 编程语言 > 详细

C++尊重返回值

时间:2014-06-05 16:02:20      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:c   style   a   color   int   get   

尊重返回值,一定要对返回值的有效性进行判断,保证程序正确执行。常用技巧:

1.指针判空 使用NULL去比较。

StAppConfig * m_pAppCfg = StAppConfig::getInstance();
if (NULL == m_pAppCfg)
{
  return -1;
}

2. 整数类型(byte, short, int, int64包括其无符号类型) 使用数值去比较。例如正确==0 错误==-1

int iRet = StAppConfig::xxxx();

if(0 != iRet)

{
  return -1;
}

 

3. 布尔类型bool,通常以真假判断

例1:

bool bRet = StAppConfig::xxxx();

if(!bRet)

{
  return -1;
}

例2:

bool init(char * szMsg)

{

     bool bRet = xxxxx();

     if (!bRet)

     {

           strncpy(szMsg, "xxxxx执行失败,请查收", n);

           return false;
     }

    

      strncpy(szMsg, "xxxxx执行成功", n);

     return true;
}

 

char szMsg[1024] = {0,};

if (!init(szMsg))
{
  return -1;
}

 

4. 浮点数的判断靠范围

float f = xxxx();

if (0.000000001>f && 0.0000000001<f)

{

       return -1;
}

 

C++尊重返回值,布布扣,bubuko.com

C++尊重返回值

标签:c   style   a   color   int   get   

原文地址:http://www.cnblogs.com/bc05007/p/3768917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!