You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cppcx/casting-c-cx.md
+14-22Lines changed: 14 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -28,14 +28,13 @@ Four different cast operators apply to [!INCLUDE[wrt](../cppcx/includes/wrt-md.m
28
28
29
29
Use `static_cast` if the code explicitly declares a relationship between the two types, and you therefore are sure that the cast should work.
30
30
31
-
```
32
-
interface class A{};
31
+
```
32
+
interface class A{};
33
33
public ref class Class1 sealed : A { };
34
34
...
35
-
A^ obj = ref new Class1(); // Class1 is an A
36
-
// You know obj is a Class1. The compiler verifies that this is possible, and in C++/CX a run-time check is also performed.
37
-
Class1^ c = static_cast<Class1^>(obj);
38
-
35
+
A^ obj = ref new Class1(); // Class1 is an A
36
+
// You know obj is a Class1. The compiler verifies that this is possible, and in C++/CX a run-time check is also performed.
37
+
Class1^ c = static_cast<Class1^>(obj);
39
38
```
40
39
41
40
## safe_cast
@@ -44,26 +43,22 @@ Class1^ c = static_cast<Class1^>(obj);
44
43
Use safe_cast if the code does not declare the relationship but you are sure that the cast should work.
45
44
46
45
```
47
-
48
46
// A and B are not related
49
47
interface class A{};
50
48
interface class B{};
51
49
public ref class Class1 sealed : A, B { };
52
50
...
53
-
A^ obj = ref new Class1();
54
-
55
-
// You know that obj’s backing type implements A and B, but
56
-
// the compiler can’t tell this by comparing A and B. The run-time type check succeeds.
57
-
B^ obj2 = safe_cast<B^>(obj);
51
+
A^ obj = ref new Class1();
58
52
53
+
// You know that obj’s backing type implements A and B, but
54
+
// the compiler can’t tell this by comparing A and B. The run-time type check succeeds.
55
+
B^ obj2 = safe_cast<B^>(obj);
59
56
```
60
57
61
58
## dynamic_cast
62
59
Use `dynamic_cast` when you cast an object (more specifically, a hat `^`) to a more derived type, you expect either that the target object might sometimes be `nullptr` or that the cast might fail, and you want to handle that condition as a regular code path instead of an exception. For example, in the **Windows Store Blank App** project template, the `OnLaunched` method in `app.xamp.cpp` uses `dynamic_cast` to test whether the app window has content. It's not an error if it doesn’t have content; it is an expected condition. `Windows::Current::Content` is a `Windows::UI::XAML::UIElement` and the conversion is to a `Windows::UI.XAML::Controls::Frame`, which is a more derived type in the inheritance hierarchy.
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
69
64
@@ -76,7 +71,6 @@ B^ obj2 = safe_cast<B^>(obj);
76
71
rootFrame = ref new Frame();
77
72
...
78
73
```
79
-
80
74
Another use of `dynamic_cast` is to probe an `Object^` to determine whether it contains a boxed value type. In this case, you attempt a `dynamic_cast<Platform::Box>` or a `dynamic_cast<Platform::IBox>`.
81
75
82
76
**dynamic_cast and tracking references (%)**
@@ -87,13 +81,11 @@ B^ obj2 = safe_cast<B^>(obj);
87
81
We recommend that you not use [reinterpret_cast](../cpp/reinterpret-cast-operator.md) because neither a compile-time check nor a run-time check is performed. In the worst case, a `reinterpret_cast` makes it possible for programming errors to go undetected at development time and cause subtle or catastrophic errors in your program’s behavior. Therefore, we recommend that you use `reinterpret_cast` only in those rare cases when you must cast between unrelated types and you know that the cast will succeed. An example of a rare use is to convert a [!INCLUDE[cppwrt](../cppcx/includes/cppwrt-md.md)] type to its underlying ABI type—this means that you are taking control of the reference counting for the object. To do this, we recommend that you use the [ComPtr Class](../cpp/com-ptr-t-class.md) smart pointer. Otherwise, you must specifically call Release on the interface. The following example shows how a ref class can be cast to an `IInspectable*`.
If you use `reinterpret_cast` to convert from one [!INCLUDE[cppwrt](../cppcx/includes/cppwrt-md.md)] interface to another, you cause the object to be released twice. Therefore, only use this cast when you are converting to a non-[!INCLUDE[cppwrt](../cppcx/includes/cppwrt-md.md)] interface.
0 commit comments