C++/CX 限制

发布于 2024-12-19 09:33:19 字数 420 浏览 1 评论 0原文

我想使用 C++/CX 语法编写一个库并在其他项目中使用它。

如您所知,普通的旧数据结构不能有任何方法或运算符,因此我们必须在另一个类中编写静态方法来提供行为。

另外,我们不能编写这样的代码:

private:
    int _D;
public:
    property int& D { int& get() {return _D;}}

或将此属性传递给这样的方法:

void SampleMethod(int& d);

我不知道为什么 Windows 运行时库有这些限制。

如何在 .lib 文件中编译 C++/CX 和 Windows 运行时扩展?或者如何在另一个项目中预编译整个 WRL 项目的代码文件?

I want to write a library using C++/CX syntax and use it in other projects.

As you know, plain old data structures cannot have any methods or operators, so we had to do things like writing static methods in another class to provide behavior.

Also we can't write code like this :

private:
    int _D;
public:
    property int& D { int& get() {return _D;}}

or pass this property to methods like this:

void SampleMethod(int& d);

I don't know why the Windows Runtime Library has these restrictions.

How can I compile C++/CX and Windows Runtime extensions in a .lib file? Or how do I precompile the whole WRL project's code files in another project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2024-12-26 09:33:19

WinRT 是一种 ABI,旨在跨语言使用 - 至少是 C++、C#、VB 和 JS。因此,它提供的构造仅限于可以用这些语言以直接方式表示的内容 - 例如,如果通过引用返回,C# 代码将如何使用它?

如果您想在 C++ 中编写静态库以仅从 C++ 中使用,我的建议是尽可能避免语言扩展,并且仅在必须传递 WinRT 对象的地方保留它们。在这种情况下,使用完整的 C++/CX 不会带来任何好处。

WinRT is an ABI that is intended for cross-language use - at least C++, C#, VB and JS. For this reason, the constructs it provides are limited to what can be represented in those languages in a straightforward way - for example, if you return by reference, how would C# code use that?

If you want to write a static library in C++ for consumption only from C++, my advice would be to avoid language extensions inasmuch as possible, and only keep them for those places where you have to pass WinRT objects around. There's no benefit you will derive in that scenario from going full C++/CX.

小嗲 2024-12-26 09:33:19

与 C++ 不同,在 C++ 中,“结构”基本上是“所有成员都是公共的类”,在 Windows 运行时中,“结构”是一种值类型。因此,它不能有任何方法,包括属性访问器(值类型不能有方法)。

此外,由于结构是值类型,因此它们始终按值传递。这意味着将结构传递到方法中需要复制结构的内容(值类型意味着每个使用者都对自己的数据类型副本进行操作)。因此,结构的大小应该相当小(认为“小于 16 字节”)。

如果您需要传递较大的对象,请改用类 - 类通过引用传递,因此可以保存任意数量的数据。

Unlike C++, where a "struct" is basically "a class where all the members are public", in the windows runtime, a "struct" is a value type. As such, it cannot have any methods, including property accessors (value types can't have methods).

In addition, because structs are value types, they are always passed by value. This means that passing a struct into a method requires copying the contents of the struct (a value type implies that every consumer operates on their own copy of the data type). As a consequence of this, structs should be fairly small in size (think "under 16 bytes").

If you need to pass around larger objects, use a class instead - classes are passed by reference and thus can hold any amount of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文