Skip to content

Commit b7b428e

Browse files
authored
Merge pull request MicrosoftDocs#21 from ChrisGuzak/patch-1
fix reinterpret_cast sample, minor formatting fixes
2 parents 3562ea9 + f6ce47a commit b7b428e

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

docs/cppcx/casting-c-cx.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ Four different cast operators apply to [!INCLUDE[wrt](../cppcx/includes/wrt-md.m
2828

2929
Use `static_cast` if the code explicitly declares a relationship between the two types, and you therefore are sure that the cast should work.
3030

31-
```
32-
interface class A{};
31+
```
32+
interface class A{};
3333
public ref class Class1 sealed : A { };
3434
...
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);
3938
```
4039

4140
## safe_cast
@@ -44,26 +43,22 @@ Class1^ c = static_cast<Class1^>(obj);
4443
Use safe_cast if the code does not declare the relationship but you are sure that the cast should work.
4544

4645
```
47-
4846
// A and B are not related
4947
interface class A{};
5048
interface class B{};
5149
public ref class Class1 sealed : A, B { };
5250
...
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();
5852
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);
5956
```
6057

6158
## dynamic_cast
6259
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.
63-
64-
```
65-
66-
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
60+
```
61+
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
6762
{
6863
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
6964
@@ -76,7 +71,6 @@ B^ obj2 = safe_cast<B^>(obj);
7671
rootFrame = ref new Frame();
7772
...
7873
```
79-
8074
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>`.
8175

8276
**dynamic_cast and tracking references (%)**
@@ -87,13 +81,11 @@ B^ obj2 = safe_cast<B^>(obj);
8781
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*`.
8882

8983
```
90-
9184
#include <wrl.h>
9285
using namespace Microsoft::WRL;
9386
auto winRtObject = ref new SomeWinRTType();
94-
ComPtr<IInspectable> inspectable = reinterpret_cast<IInspectable*>(obj2);
95-
...
96-
87+
ComPtr<IInspectable> inspectable = reinterpret_cast<IInspectable*>(winRtObject);
88+
...
9789
```
9890

9991
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.
@@ -126,4 +118,4 @@ ComPtr<IInspectable> inspectable = reinterpret_cast<IInspectable*>(obj2);
126118
## See Also
127119
[Type System](../cppcx/type-system-c-cx.md)
128120
[Visual C++ Language Reference](../cppcx/visual-c-language-reference-c-cx.md)
129-
[Namespaces Reference](../cppcx/namespaces-reference-c-cx.md)
121+
[Namespaces Reference](../cppcx/namespaces-reference-c-cx.md)

0 commit comments

Comments
 (0)