栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++项目中代码改进点

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++项目中代码改进点

  • 包含头文件时,推荐使用c++风格
    详细讨论见知乎帖子:https://www.zhihu.com/question/51288493
    看下来一句话:都可以,c++风格代码风格会优雅些
 #include   // bad
 #include  // good
  • 非必要情况下,不使用无符号类型
    主要遵循c++ google style:

    You should not use the unsigned integer types such as uint32_t, unless there is a valid reason such as representing a bit pattern rather than a number, or you need defined overflow modulo 2^N. In particular, do not use unsigned types to say a number will never be negative. Instead, use assertions for this.

    无符号类型与有符号类型数字在一起运算时,结果未知:

     

    下溢导致死循环

     for(unsigned int i = 100; i >= 0; i--) {
        Print(array[i]);
    }
    
  • 定义enum时,使用enum class

  • 定义宏函数时,使用do {}while(0);方式

  • add_definitions是全局的,会污染其他cmake文件。可以使用set_target_properties给编译目标添加编译选项

  • 默认就不加SHARED了,需要链接动态库的时候可以设一个全局的BUILD_SHARED_LIBS标记

  • 全局对象建议放到匿名空间使其不污染全局命名空间

  • c++代码中的struct关键字可以省略

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/317410.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号