您的位置:首页 > 编程语言 > Go语言

goto语句的“跳跃”问题

2013-08-06 11:10 204 查看
   在对节目信息添加到数据库的线程中,使用了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.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐