连goto都解决不了的问题, 那就用do while(0)来解决吧!
来源:互联网 发布:算法流程图 编辑:程序博客网 时间:2024/06/01 08:30
之前在博文中说过do while(0)的用法, 不过实际项目中用得不多。 最近遇到麻烦了, 感觉到不用do while(0)就很蛋疼。
先来看一段比较常规的丑代码:
#include<iostream>using namespace std;int fun(int a, int b, int c, int d){int errCode = 0;if(a < 0){errCode = -1;printf("error1\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r4 = 0;// add normal business logic hereprintf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int main(){fun(-1, -1, -1, -1);return 0;}我们可以看到, 代码存在极大的冗余, 一坨一坨的, 像什么样! 于是, 有的人说用goto可以搞定啊。 且不说goto为人诟病已久, 先来看看goto的效果:
#include<iostream>using namespace std;int fun(int a, int b, int c, int d){int errCode = 0;if(a < 0){errCode = -1;printf("error1\n");goto END;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");goto END;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");goto END;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");goto END;}int r4 = 0;// add normal business logic hereEND:printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int main(){fun(-1, -1, -1, -1);return 0;}经验比较丰富的同学一眼就可以看出问题, 这里存在编译问题。 在linux GCC下提示:crosses initialization of xxx, 在Windows VC++下提示:initialization of 'r4' is skipped by 'goto END'
那怎么办呢? 也不是完全么有办法, 方法1是把所有被跳过的变量放在最前面定义, 方法2是直接用大括号把被掉过的变量括起来。 在实际中, 这两种方法我都玩过。 第一种方法非常繁琐(当时搞一个安全问题, 写代码的时候用了这个方式), 要重新移动变量的位置, 而第二种方法显得很猥琐(当时是搞一个节日活动的问题, 写代码的时候用了这种方式), 谁用谁知道。
还是来看看do while(0)的实现方式吧, 我最近就是这么搞的, 相对较爽:
#include<iostream>using namespace std;int fun(int a, int b, int c, int d){int errCode = 0;do {if(a < 0){errCode = -1;printf("error1\n");break;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");break;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");break;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");break;}int r4 = 0;}while(0);// add normal business logic hereprintf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int main(){fun(-1, -1, -1, -1);return 0;}搞定!
1 0
- 连goto都解决不了的问题, 那就用do while(0)来解决吧!
- 用钱能解决的问题,我都解决不了
- do while(0)的另一个用途:消除goto语句。
- do while(0)的另一个用途:消除goto语句
- C语言中do...while(0)的妙用-避免goto
- 解决不了的问题
- 解决代码逻辑嵌套过多的技巧 do{} while(0);
- 用for循环或while循环来解决百钱买百鸡问题
- tomcat6.0启动不了问题的解决
- 实在解决不了的问题
- 待解决(永远解决不了)的问题
- 从头认识多线程-3.3 synchronized某些解决不了的可视性问题,只能使用volatile来解决
- 使用do..while 替换 goto
- 程序问题,就没有解决不了的问题,show me source code
- 使用了PYG解密小组的Visual Assist X,就用不了其他的 ,问题分析解决
- 解决PL/SQL客户端连oracle很快就断开的问题
- 单出口函数的实现方式(goto do while)
- 用do-while循环巧妙解决n个n相乘
- 通过Samba让Windows访问Linux文件
- Python ConfigParser 注意事项
- linux开启防火墙端口和查看,开启相关端口号
- liblinear比libsvm快的原因
- UVM的各种port(六)
- 连goto都解决不了的问题, 那就用do while(0)来解决吧!
- hive UDF UDAF
- Django admin 的整理
- Java NIO学习笔记
- 一个简单的struts2程序
- Leetcode #453 Minimum Moves to Equal Array Elements
- UVM的register model介绍(七)
- Open Course
- 节点重要性和相似性