سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top