代码之家  ›  专栏  ›  技术社区  ›  Pietro

C++中多行代码中的注释

  •  1
  • Pietro  · 技术社区  · 6 年前

    代码不应该:

    int Func(int a, // comment
             int b, // comment
             int c  // comment
            ) ...
    

    int Func(int a, // comment int b, // comment int c  // comment) ...
    

    为什么它能正确构建(至少使用G++)?

    到目前为止,我一直用 /* */

    4 回复  |  直到 6 年前
        1
  •  6
  •   NathanOliver    6 年前
    int Func(int a, // comment
             int b, // comment
             int c  // comment
            ) ...
    

    转换为

    int Func(int a,  
             int b,  
             int c   
            ) ...
    

    third phase of translation /* */ 喜欢

        int Func(int a /*comment 1*/, int b /*comment 2*/, int c /*comment 3*/ ) ...
    
        2
  •  4
  •   muXXmit2X    6 年前

    standard

    字符//开始一个注释,该注释立即终止

    因此,在删除注释之后,编译器最终解释的代码如下所示:

    int Func(int a,
             int b,
             int c 
            ) {}
    

        3
  •  1
  •   463035818_is_not_an_ai    6 年前

    注释与代码无关,因此此代码:

    int Func(int a, // comment
             int b, // comment
             int c  // comment
            ) {}
    

    实际上相当于:

    int Func(int a,
             int b,
             int c 
            ) {}
    

    或者如果你也想要这个:

    int Func(int a,int b,int c) {}
    

    // 结束于行的末尾,因此将代码与注释放在同一行会将代码转换为注释,而您的两个代码片段是不等价的。

        4
  •  0
  •   Jabberwocky    6 年前

    • 以开头的评论 // 在行尾终止
    • 以开头的评论 /* 在下一个 */

    最初的C规范只知道 /* */ 风格评论。

    这个 // 样式注释是用C++来介绍的,也在C标准中介绍(不确定哪一个)。

    所以这个:

    int Func(int a, // comment
             int b, // comment
             int c  // comment
            ) ...
    

    相当于:

    int Func(int a, /* comment */
             int b, /* comment */
             int c  /* comment */
            ) ...
    

    int Func(int a, /* comment */ int b, /* comment */ int c  /* comment */) ...