Skip to content

Commit 9600154

Browse files
authored
Stwhi main/updating code (#3802)
* updating code * tidying up code * tidying up code 2 * tidying up code 3 * mentioning m_inner
1 parent 5e52a4d commit 9600154

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

uwp/cpp-and-winrt-apis/author-apis.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: This topic shows how to author C++/WinRT APIs by using the **winrt::implements** base struct, either directly or indirectly.
33
title: Author APIs with C++/WinRT
4-
ms.date: 09/22/2023
4+
ms.date: 09/28/2023
55
ms.topic: article
66
keywords: windows 10, uwp, standard, c++, cpp, winrt, projected, projection, implementation, implement, runtime class, activation
77
ms.localizationpriority: medium
@@ -288,7 +288,10 @@ IStringable istringable = winrt::make<MyType>();
288288
> [!NOTE]
289289
> However, if you're referencing your type from your XAML UI, then there will be both an implementation type and a projected type in the same project. In that case, **make** returns an instance of the projected type. For a code example of that scenario, see [XAML controls; bind to a C++/WinRT property](binding-property.md#add-a-property-of-type-bookstoreviewmodel-to-mainpage).
290290
291-
We can only use `istringable` (in the code example above) to call the members of the **IStringable** interface. But a C++/WinRT interface (which is a projected interface) derives from [**winrt::Windows::Foundation::IUnknown**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown). So, you can call [**IUnknown::as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function) (or [**IUnknown::try_as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function)) on it to query for other projected types or interfaces, which you can also either use or return.
291+
We can use `istringable` (in the code example above) only to call the members of the **IStringable** interface. But a C++/WinRT interface (which is a projected interface) derives from [**winrt::Windows::Foundation::IUnknown**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown). So, you can call [**IUnknown::as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function) (or [**IUnknown::try_as**](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function)) on it to query for other projected types or interfaces, which you can also either use or return.
292+
293+
> [!TIP]
294+
> A scenario where you *shouldn't* call **as** or **try_as** is runtime class derivation ("composable classes"). When an implementation type composes another class, don't call **as** or **try_as** in order to perform an unchecked or checked **QueryInterface** of the class being composed. Instead, access the (`this->`) `m_inner` data member, and call **as** or **try_as** on that. For more info, see [Runtime class derivation](#runtime-class-derivation) in this topic.
292295
293296
```cppwinrt
294297
istringable.ToString();
@@ -495,6 +498,9 @@ namespace winrt::MyProject::implementation
495498
}
496499
```
497500

501+
> [!TIP]
502+
> When an implementation type composes another class, don't call **as** or **try_as** in order to perform an unchecked or checked **QueryInterface** of the class being composed. Instead, access the (`this->`) `m_inner` data member, and call **as** or **try_as** on that.
503+
498504
## Deriving from a type that has a non-default constructor
499505

500506
[**ToggleButtonAutomationPeer::ToggleButtonAutomationPeer(ToggleButton)**](/uwp/api/windows.ui.xaml.automation.peers.togglebuttonautomationpeer.-ctor#Windows_UI_Xaml_Automation_Peers_ToggleButtonAutomationPeer__ctor_Windows_UI_Xaml_Controls_Primitives_ToggleButton_) is an example of a non-default constructor. There's no default constructor so, to construct a **ToggleButtonAutomationPeer**, you need to pass an *owner*. Consequently, if you derive from **ToggleButtonAutomationPeer**, then you need to provide a constructor that takes an *owner* and passes it to the base. Let's see what that looks like in practice.
@@ -843,6 +849,7 @@ This requires that all members of the class hierarchy agree on the return value
843849
* [winrt::make function template](/uwp/cpp-ref-for-winrt/make)
844850
* [winrt::make_self function template](/uwp/cpp-ref-for-winrt/make-self)
845851
* [winrt::Windows::Foundation::IUnknown::as function](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknownas-function)
852+
* [winrt::Windows::Foundation::IUnknown::try_as function](/uwp/cpp-ref-for-winrt/windows-foundation-iunknown#iunknowntry_as-function)
846853

847854
## Related topics
848855
* [Author events in C++/WinRT](./author-events.md)

0 commit comments

Comments
 (0)