简体   繁体   English

Winui 3 winrt C++ 引用计数

[英]Winui 3 winrt C++ reference counting

Using WinUI 3, Winrt C++ in the following code:在以下代码中使用 WinUI 3、Winrt C++:
Is myRectangle reference counted? myRectangle 引用是否被计算在内? Or is it copied?还是抄袭?

namespace winrt::MyProject::implementation{

void MyProjectClass::fnc(){

winrt::Microsoft::UI::Xaml::Controls::Canvas myCanvas = CanvasElementFromXAML();
winrt::Microsoft::UI::Xaml::Shapes::Rectangle myRectangle;
myCanvas.Children.Append(myRectangle);

}
}

I see this type of code in examples.我在示例中看到了这种类型的代码。 In C++ (without MS extensions) this would lead to a dangling reference/pointer if not copied, since myRectangle is a local var which will go out of scope.在 C++(没有 MS 扩展)中,如果不复制,这将导致悬空引用/指针,因为 myRectangle 是一个将超出范围的本地变量。 myCanvas.Children.Append() receives UIElement const& value myCanvas.Children.Append() 接收 UIElement const& 值
void Append(UIElement const& value) 无效附加(UIElement const& 值)

here the const reference would communicate that the argument cannot be changed, therefore ref count cannot be modified.这里 const 引用将传达参数无法更改,因此无法修改 ref 计数。

WinRT is COM-based , so the Canvas instance and the Rectangle instance are both COM-objects. WinRT 是基于 COM 的,因此Canvas实例和Rectangle实例都是 COM 对象。

So, Append implementation will call AddRef on myRectangle .因此, Append实现将在myRectangle上调用AddRef In this case, there's no specific MS extension involved.在这种情况下,不涉及特定的 MS 扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM