Chinaunix首页 | 论坛 | 博客
  • 博客访问: 840294
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: LINUX

2012-08-01 22:19:21


点击(此处)折叠或打开

  1. switch (choice) {
  2.   case CHOICE_A:
  3.     someclass obj1(&commonobj);

  4.     break;
  5.   case CHOICE_B:
  6.     someotherclass obj2(&commonobj);

  7.     break;
  8.   default:
  9.     break;
  10.   }
  11. }
Reason: The problem is that there is a declaration of an object (the boolean "pushed") without scope. Thus, the scope of the object could traverse the break statement and apply to the next case. Consider this - what is the scope of obj1 in the code below? It starts at the first label, and goes until the end of the case block. So it's in scope at CHOICE_B. But its constructor wasn't called....

点击(此处)折叠或打开

  1. switch (choice) {
  2.   case A: {
  3.     someobj x;
  4.     ...
  5.   }
  6.   break;
  7.   case B: {
  8.     ...
  9.   }
  10.   break;
  11.   ...
  12. }


阅读(2631) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~