前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.148:使用dynamic_cast进行指针类型转换时,将不能发现目标类看作是有效的选项

C++核心准则C.148:使用dynamic_cast进行指针类型转换时,将不能发现目标类看作是有效的选项

作者头像
面向对象思考
发布2020-03-25 16:55:56
8890
发布2020-03-25 16:55:56
举报

C.148: Use dynamic_cast to a pointer type when failure to find the required class is considered a valid alternative

C.148:使用dynamic_cast进行指针类型转换时,将不能发现目标类看作是有效的选项

Reason(原因)

The dynamic_cast conversion allows to test whether a pointer is pointing at a polymorphic object that has a given class in its hierarchy. Since failure to find the class merely returns a null value, it can be tested during run time. This allows writing code that can choose alternative paths depending on the results.

dynamic_cast转换允许检查是否指针指向一个在其继承结构中包含给定类的多态对象。由于转换失败的结果仅仅是返回一个空值,这个结果可以在执行时检查。这个特性允许根据结果选择不同的路径。

Contrast with C.147, where failure is an error, and should not be used for conditional execution.

和C.147不同,那里的失败是错误,而且不应该被用于条件执行。

Example(示例)

The example below describes the add function of a Shape_owner that takes ownership of constructed Shape objects. The objects are also sorted into views, according to their geometric attributes. In this example, Shape does not inherit from Geometric_attributes. Only its subclasses do.

下面的例子描述的是Shape_owner的增加函数,它接受构造出来的Shape对象的所有权。对象也会在根据它们的几何属性有序加入views容器。在这个例子中,图形没有从几何属性继承。只有它的子类这么做了。

void add(Shape* const item)
{
  // Ownership is always taken
  owned_shapes.emplace_back(item);

  // Check the Geometric_attributes and add the shape to none/one/some/all of the views

  if (auto even = dynamic_cast<Even_sided*>(item))
  {
    view_of_evens.emplace_back(even);
  }

  if (auto trisym = dynamic_cast<Trilaterally_symmetrical*>(item))
  {
    view_of_trisyms.emplace_back(trisym);
  }
}
Notes(注意)

A failure to find the required class will cause dynamic_cast to return a null value, and de-referencing a null-valued pointer will lead to undefined behavior. Therefore the result of the dynamic_cast should always be treated as if it may contain a null value, and tested.

寻找所需类的失败会导致dynamic_cast返回一个空值,而解引用一个空指针会引起无定义的行为。因此应该总是认为dynamic_cast的结果可能为空并进行检查。

Enforcement(实施建议)
  • (Complex) Unless there is a null test on the result of a dynamic_cast of a pointer type, warn upon dereference of the pointer.
  • (复杂) 如果在dynamic_cast执行之后,没有对结果指针进行空判断,那么对使用这个指针的代码报警。

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c148-use-dynamic_cast-to-a-pointer-type-when-failure-to-find-the-required-class-is-considered-a-valid-alternative


觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C.148: Use dynamic_cast to a pointer type when failure to find the required class is considered a valid alternative
    • Reason(原因)
      • Example(示例)
        • Notes(注意)
          • Enforcement(实施建议)
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档