当前位置: 首页 > news >正文

5.C++程序中的注释

我们来看上节所写的程序

#include <iostream>
using namespace std;void prnt()  //打印A
{cout << "printA" << endl;
}int main()
{prnt();return 0;
}

上面的程序中“//打印A”,表示说明当前函数是打印内容的函数,具体打印结果,写了一个A,没有再进行更详细的描述,这个时候,我们看到的时候,能大概了解一下这个函数是做什么的,但是还得要执行函数才能知道结果,那么 ”//“后面的内容就是注释。

在编程时,注释是一个非常实用的一个工具或者是功能,可以极大提高代码的可读性和可维护性,上面的函数比较简单,可能看一眼就知道是什么意思,但是如果写一个功能复杂的函数或者是比较大的函数,看一下下面的函数(非C++代码,实际工作时写的方法)

 public TmsDistributionLineVo autoDistributionLine(String accessId, BasePickUpPoint point, String orderChannel) {try {/*** 获取距离最近的点*/MPJQueryWrapper<TmsDistributionLine> mpjQW = null;/*** 获取距离最近的线*/mpjQW = new MPJQueryWrapper<TmsDistributionLine>();String sql = StrUtil.format(" st_distance_sphere(point({},{}), point(lng_center,lat_center)) distance ",point.getLongitude(), point.getLatitude());mpjQW.select(" id,distribution_line_name," + sql);mpjQW.lambda().eq(TmsDistributionLine::getLogisticsAreaId, point.getLogisticsAreaId());mpjQW.eq("access_id", accessId);mpjQW.eq("is_del", 0);mpjQW.eq("flag", 1);mpjQW.lambda().eq(TmsDistributionLine::getOrderChannel, orderChannel);mpjQW.orderByAsc("distance");mpjQW.last(" limit 1");TmsDistributionLineVo line = this.selectJoinOne(TmsDistributionLineVo.class, mpjQW);if (line != null && line.getDistance().doubleValue() < 1000) { } else {mpjQW = new MPJQueryWrapper<TmsDistributionLine>();sql = StrUtil.format(" st_distance_sphere(point({},{}), point(longitude,latitude)) distance ",point.getLongitude(), point.getLatitude());mpjQW.innerJoin(" tms_distribution_line_delivery_point_ref  b on t.id = b.distribution_line_id ");mpjQW.innerJoin(" base_pick_up_point  c on c.id = b.delivery_point_id ");mpjQW.select("t.id, t.distribution_line_name," + sql);mpjQW.lambda().eq(TmsDistributionLine::getLogisticsAreaId, point.getLogisticsAreaId());mpjQW.lambda().eq(TmsDistributionLine::getAccessId, accessId).eq(TmsDistributionLine::getIsDel, 0).eq(TmsDistributionLine::getFlag, 1);mpjQW.lambda().eq(TmsDistributionLine::getOrderChannel, orderChannel);mpjQW.orderByAsc("distance");mpjQW.last(" limit 1");TmsDistributionLineVo vo = this.selectJoinOne(TmsDistributionLineVo.class, mpjQW);//if (line == null && vo != null)line = vo;else {if (vo != null && vo.getDistance().doubleValue() < line.getDistance().doubleValue()) //{line = vo;}}}distributionLineDeliveryPointRefService.autoSaveRef(point.getLogisticsAreaId(), line.getId(), point.getId(), accessId, orderChannel);return line;} catch (Exception ex) {log.error(ex.getMessage());return null;}

 我们写的时候,可以感觉此代码挺简单,功能也想很清楚,但是等过一段时间,再返回来看此代码,就有点摸不着头脑了,得需要一点点去分析,去执行调试才能知道是什么意思。而且如果让其它人看此代码,可能更不清楚代码是什么意思,需要给其它人解析哪一段是什么意思,执行之后,将产生什么结果等等。不利用程序的维护,可读性差。

在C++代码中的注释语法有两种,单行注释(//)和多行注释(/*....*/)。在编译的时候,编译器会自动忽略注释的内容。

单行注释(//)

单行注释用于注释一行代码,比如对某一个变量的说明,如上面的

void prnt()  //打印A

//打印A就是单行注释。

多行注释主要用于注释整个代码块,在注释中可以写明当前注释的内容,或者是介绍当前函数的意义,实现方式,返回数据的类型等,也可以注释掉整个代码块。

#include <iostream>
using namespace std;/*** 主要用于实现打印功能,打印的内容为printA * * @author 码农豆豆* @version v1.0 * * @return 无返回* * **/
void prnt()  
{cout << "printA" << endl;
}int main()
{prnt();return 0;
}

上面的注释就是典型的多行注释,用于解释当前函数的功能,作者,当前函数的版本号等等。

使用单行注释也可以完成,只是写起来比较累,而且很多不明确,不如多行注释规范和明确,如果当前函数暂时不需要,也可以注释掉

#include <iostream>
using namespace std;/*** 主要用于实现打印功能,打印的内容为printA * * @author 码农豆豆* @version v1.0 * * @return 无返回* * **//* 暂时不需要,注释掉
void prnt()  
{cout << "printA" << endl;
}
*/int main()
{//prnt();return 0;
}

函数注释后,在main中的引用也得注释了,否则就会找不到这个函数,提示没有定义。在main中,使用了单行注释。

所以在程序中写注释是一个很重要的事情,如果不写注释,将大大降低程序的可性读和易维护性。因此一定要养成顺手写注释的习惯

http://www.lryc.cn/news/440450.html

相关文章:

  • com.kingbase8.util.KSQLException: ERROR: permission denied for table xxx
  • 开发小程序
  • JS考核答案
  • 高德地图2.0 绘制、编辑多边形覆盖物(电子围栏)
  • MySQL底层为什么选择用B+树作为索引
  • MATLAB系列05:自定义函数
  • C++速通LeetCode简单第20题-多数元素
  • 回收站永久删除的文件还能恢复吗?教你恢复技巧
  • Python Web 微服务架构全面解析与实战指南
  • SEAFARING靶场漏洞攻略
  • ROS 编程入门的介绍
  • 第十一章 抽象类与接口
  • 请问企业的八大金刚系统是哪些?有什么共同点和区别?
  • 【入门】配置 Java 应用程序的完整指南
  • flutter widget 设置GestureDetector点击无效
  • 基于SpringBoot的在线教育平台的设计与实现
  • Django_Vue3_ElementUI_Release_004_使用nginx部署
  • Java抽象类的案例
  • 运维工程师面试整理-数据库
  • comfyui一键抠图工作流:让你告别PS!
  • 【Hot100】LeetCode—4. 寻找两个正序数组的中位数
  • 【LLM text2sql】浅看大模型用于text2sql的综述
  • Node js介绍
  • 企业编辑抖音百科词条有什么用?
  • 数据结构-链式二叉树-四种遍历
  • 【YashanDB知识库】数据库获取时间和服务器时间不一致
  • 十大排序之:冒泡排序
  • 【MPC】无人机模型预测控制复现Data-Driven MPC for Quadrotors项目(Part 1)
  • 微信小程序开发——比较两个数字大小
  • Java多线程3