返回资讯中心
外部精选
软件工程
#开发者实时流

One way of thinking about access specifiers in C++/WinRT implementation types

In C++/WinRT, Windows Runtime classes are exposed to the outside world through the projection, but internally they are represented by their implementation type. Since the outside world sees only the projection, implemen…

Hacker News 最新ibobev2026/10/19 · 29 天前4 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

In C++/WinRT, Windows Runtime classes are exposed to the outside world through the projection, but internally they are represented by their implementation type. Since the outside world sees only the projection, implementation types are visible only to the module that produced them.

This realization lets you reinterpret the C++ access specifiers for C++/WinRT implementation types.

struct Widget : winrt::implements<Widget, IStringable> { public: // This is a member of IStringable, // so it is accessible to anyone. winrt::hstring ToString();

// This does not implement any member from IStringable, // so it is accessible to other code in the same module. void Recolor(winrt::Windows::UI::Color newColor);

protected: // Accessible to implementation classes that derive from Widget. void Reverse();

private: // Accessible only to Widget itself. void Reset(); };

Note that if you are implementing a runtime class, then C++/WinRT autogenerates a template for you that auto-declares all of the declared interfaces of that class.

// Widget.idl

namespace Contoso { runtimeclass Widget : Windows.Foundation.IStringable { String Name{ get; }; void Toggle(); } }

The IDL compiler autogenerates an interface IWidget with a property Name and a method Toggle().

// implementation namespace winrt::Contoso { struct Widget : WidgetT<Widget> { ⟦ ... ⟧ }; }

The WidgetT<Widget> expands to implements<Widget, IWidget, Windows::Foundation::IStringable> because those are the interfaces declared by the IDL file.

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →