goto语句的“跳跃”问题

在对节目信息添加到数据库的线程中,使用了Goto语句,出现error C2362: initialization of ‘var’ is skipped by ‘goto ItemAdd’。这句话是指在执行
goto语句时跳过了变量var的初始化,原因在于C/C++规定,当程序从一个程序块1(局部变量var未定义),使用goto语句跳跃到程序块2的时候,
如果此时该变量已经声明且已经定义,这时候编译器就会报错。这种情况对于switch–Case的情况也适用,其运行机制与Goto是相同的。在本程序中,
变量var(_variant_t)的定义在goto之后,在跳转到的语句块之前被声明,因此编译器就会出现编译错误。
小结:
1. 使用goto语句时对POD对象进行操作时,该在goto使用之前进行定义,
避免在goto语句和跳转程序块之间声明该语句
2. switch-case语句而言,应该将各个Case语句用{…}引用起来,使其形成一个独立的程序模块
具体规定参照:
C++标准规定:
It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer. (The transfer from the condition of a switch statement to a case label is considered a jump in this respect.)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值