Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则​CPL.1:C++比C更好​

C++核心准则​CPL.1:C++比C更好​

作者头像
面向对象思考
发布于 2020-10-10 02:02:40
发布于 2020-10-10 02:02:40
44500
代码可运行
举报
运行总次数:0
代码可运行

CPL.1: Prefer C++ to C

CPL.1:C++比C更好

Reason(原因)

C++ provides better type checking and more notational support. It provides better support for high-level programming and often generates faster code.

C++提供更好的类型检查和更多的记法支持。它为高层次编程提供更好的支持,通常也会产生更快的代码。

Example(示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
char ch = 7;
void* pv = &ch;
int* pi = pv;   // not C++
*pi = 999;      // overwrite sizeof(int) bytes near &ch

The rules for implicit casting to and from void* in C are subtle and unenforced. In particular, this example violates a rule against converting to a type with stricter alignment.

C语言中有关void*的隐式类型转换的规则含糊且无用。更加严重的是,这个例子违反了防止使用直接的赋值实现类型转换的规则。

Enforcement(实施建议)

Use a C++ compiler.

使用C++编译器

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cpl1-prefer-c-to-c

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-10-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则ES.64:使用T{e}记法构造对象
The T{e} construction syntax makes it explicit that construction is desired. The T{e} construction syntax doesn't allow narrowing. T{e} is the only safe and general expression for constructing a value of type T from an expression e. The casts notations T(e) and (T)e are neither safe nor general.
面向对象思考
2020/05/29
5910
C++核心准则ES.64:使用T{e}记法构造对象
C++核心准则​CPL.2:如果你必须使用C,使用C和C++的共同子集,并且使用C++编译器编译C代码
CPL.2: If you must use C, use the common subset of C and C++, and compile the C code as C++
面向对象思考
2020/10/10
7580
C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好
Readability. Direct expression of an idea.
面向对象思考
2020/08/27
4660
C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好
C++核心准则C.133:避免保护型数据成员‍
protected data is a source of complexity and errors.protected data complicates the statement of invariants.protected data inherently violates the guidance against putting data in base classes, which usually leads to having to deal with virtual inheritance as well.
面向对象思考
2020/03/25
4750
C++核心准则ES.48:避免使用类型转换
Casts are a well-known source of errors. Make some optimizations unreliable.
面向对象思考
2020/05/25
6740
C++核心准则ES.48:避免使用类型转换
C++核心准则C.131: 避免无意义的getters和setters‍
A trivial getter or setter adds no semantic value; the data item could just as well be public.
面向对象思考
2020/03/25
5820
C++核心准则C.46:默认状态下明确定义单参数构造函数
C.46: By default, declare single-argument constructors explicit
面向对象思考
2020/03/25
6330
C++核心准则​:注释风格
Compilers do not read comments. Comments are less precise than code. Comments are not updated as consistently as code.
面向对象思考
2020/11/26
5120
C++核心准则T.120:只在确实有需要时使用模板元编程
T.120: Use template metaprogramming only when you really need to
面向对象思考
2020/09/28
5210
C++核心准则CP.9:尽可能使用工具检查并发代码
CP.9: Whenever feasible use tools to validate your concurrent code
面向对象思考
2020/07/02
4030
C++核心准则​​ES.61:使用delete[]销毁数组,使用delete销毁对象
That's what the language requires and mistakes can lead to resource release errors and/or memory corruption.
面向对象思考
2020/05/25
9630
C++核心准则​​ES.61:使用delete[]销毁数组,使用delete销毁对象
C++核心准则T.​81:不要混用继承层级和数组
An array of derived classes can implicitly "decay" to a pointer to a base class with potential disastrous results.
面向对象思考
2020/09/28
4510
C++核心准则C.146:如果无法避免在继承层次中移动,使用dynamic_cast
Use of the other casts can violate type safety and cause the program to access a variable that is actually of type X to be accessed as if it were of an unrelated type Z:
面向对象思考
2020/03/25
7090
C++核心准则​NL.10:首选下划线风格名称
The use of underscores to separate parts of a name is the original C and C++ style and used in the C++ Standard Library.
面向对象思考
2020/11/26
4630
C++核心准则T.43: 定义别名时,using比typedef更好
Improved readability: With using, the new name comes first rather than being embedded somewhere in a declaration. Generality: using can be used for template aliases, whereas typedefs can't easily be templates. Uniformity: using is syntactically similar to auto.
面向对象思考
2020/09/10
4840
C++核心准则ES.42: 使用指针时要简单且直接
Complicated pointer manipulation is a major source of errors.
面向对象思考
2020/05/20
4600
C++核心准则C.66:保证移动操作不会抛出异常
A throwing move violates most people's reasonably assumptions. A non-throwing move will be used more efficiently by standard-library and language facilities.
面向对象思考
2020/03/25
5920
C++核心准则Per.7:设计要为优化做准备
Because we often need to optimize the initial design. Because a design that ignores the possibility of later improvement is hard to change.
面向对象思考
2020/06/24
4560
C++核心准则C.42:如果构造函数不能生成合法对象就抛出异常
Leaving behind an invalid object is asking for trouble.
面向对象思考
2020/03/25
5930
C++核心准则ES.1: 标准库好于其他库和手写代码
ES.1: Prefer the standard library to other libraries and to "handcrafted code"
面向对象思考
2020/04/16
4190
推荐阅读
相关推荐
C++核心准则ES.64:使用T{e}记法构造对象
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验