You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio中为同组变量批量添加共享注释?

在Visual Studio中实现批量变量注释继承

当在类中声明同一任务的变量时,希望采用批量注释方式避免代码冗余——连续声明5个int类型变量,仅为第一个变量添加注释描述,其余4个自动继承该注释,鼠标悬停任意变量时均显示该注释。已知Doxygen可通过如下方式实现:

/*! 
\name This will be the description for the following group of variables
          It can be arbitrarily long, but the first line will show up in bold,
          and any subsequent lines will show up like a description under it
*/
//@{
int relatedVariable1;
int relatedVariable2;
char* relatedVariable3;
//@}

可以实现,有两种常用方式:

  • 方式一:直接复用Doxygen语法(VS原生支持)
    Visual Studio 2017及以后版本原生支持部分Doxygen语法,你可以直接使用熟悉的Doxygen分组注释写法。只需提前在VS中启用Doxygen支持:

    1. 打开Visual Studio,依次点击「工具」→「选项」→「文本编辑器」→「C/C++」→「高级」
    2. 在「生成文档注释」区域,将「Doxygen支持」设置为「是」
      配置完成后,使用上述Doxygen代码写法,鼠标悬停任意被//@{和//@}包裹的变量时,都会显示上方的注释内容。
  • 方式二:使用VS XML注释分组
    如果偏向VS原生的XML注释风格,可通过<summary>配合引用标记实现:

/// <summary>
/// 这是一组用于同一任务的变量注释,第一行是加粗标题
/// 后续内容会作为描述显示在下方
/// </summary>
int relatedVariable1;
int relatedVariable2; /// <see cref="relatedVariable1"/>
int relatedVariable3; /// <see cref="relatedVariable1"/>
int relatedVariable4; /// <see cref="relatedVariable1"/>
int relatedVariable5; /// <see cref="relatedVariable1"/>

这种方式需要给后续变量添加/// <see cref="relatedVariable1"/>来引用第一个变量的注释,相比Doxygen语法稍显繁琐,但无需额外配置VS。

内容的提问来源于stack exchange,提问作者Jasmine Kakarva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 10:27:27