Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ uses dynamic_cast

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how C++ uses dynamic_cast". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how C++ uses dynamic_cast".

C.148: when using dynamic_cast for pointer type conversion, the target class cannot be found as a valid option

Reason (reason)

The dynamic_cast transformation allows you to check whether the pointer points to a polymorphic object that contains a given class in its inheritance structure. Since the result of a failed conversion simply returns a null value, this result can be checked at execution time. This feature allows you to choose different paths based on the results. Unlike C.147, the failure there is an error and should not be used for conditional execution.

Example (sample)

The following example describes the add-on function of Shape_owner, which accepts ownership of the constructed Shape object. Objects are also added to the views container sequentially according to their geometric properties. In this example, the drawing does not inherit from geometric properties. Only its subclasses do so.

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 (item))

{

View_of_evens.emplace_back (even)

}

If (auto trisym = dynamic_cast (item))

{

View_of_trisyms.emplace_back (trisym)

}

}

Notes (Note)

Failure to find the desired class causes dynamic_cast to return a null value, while dereferencing a null pointer causes undefined behavior. Therefore, you should always assume that the result of the dynamic_cast may be null and check.

Enforcement (implementation recommendations)

(complex) if no null judgment is made on the result pointer after the execution of the dynamic_cast, an alarm is given to the code that uses the pointer.

At this point, I believe you have a deeper understanding of "C++ how to use dynamic_cast". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report