【问题标题】:How to properly create a property to store any amount of values?如何正确创建一个属性来存储任意数量的值?
【发布时间】:2013-07-15 13:03:34
【问题描述】:

从 C++ 转换到 C++/CX 我偶然发现 ref 类不能在它们是公共或受保护的成员中包含本机成员,因为 java 和其他东西可能存在错误。相反,我们现在必须使用属性,我可以制作但只能保存 1 个值...

这个想法是创建一个在数组或向量中存储 4 个浮点数的属性,然后将值传递给 XMVECTOR。到目前为止我在类头文件中的相关代码是:

public:
property std::vector<float> num{
        void set(std::vector<float> e){
            NUM = e;
        };
        std::vector<float> get(){
            return NUM;
        };
    };
private:
std::vector<float> NUM;

稍后在 .cpp 文件中我会这样做:

std::vector<float> g;
g.pushback(3);
num = g;

我也将它作为一个字符串传递给 TextBox(但这并不重要)。最后我得到了很多类似的错误...... 2个错误是:

error C3986: 'set': signature of public member contains native type 'std::vector<_Ty>'
error C3986: 'set': signature of public member contains native type 'std::allocator<_Ty>'

我唯一想到的是我不能使用字符串或向量。我知道 Platform::Strings 存在,但是向量呢??

【问题讨论】:

    标签: visual-c++ c++11 windows-runtime directx-11 c++-cx


    【解决方案1】:

    标准 C++ 类型无法跨 WinRT ABI 进行投影,WinRT ABI 是所有 WinRT 语言投影 (C#/VB/JS) 共享的通信层。正如 Jagannath 提到的,有一个集合接口 (Windows::Foundation::Collections::IVector&lt;T&gt;)。还有一个字典类型(IMap&lt;K,V&gt;)和各种迭代器和支持类型。所有语言都可以理解这些,但只是接口。每个语言投影都负责编写实现这些接口的运行时类。对于 C++/Cx,这些 ref 类位于标头 &lt;collection.h&gt; 中,并且位于命名空间 Platform::Collections 中。 Platform::Collections::Vector&lt;T&gt;Platform::Collections::Map&lt;K,V&gt; 是可以用作后备存储的基本类型。此外,Vector&lt;T&gt; 可以从std::vector&lt;T&gt; 移动构造。

    但是,您也不能创建 Platform::Collections::Vector&lt;T&gt; 类型的公共属性,因为这仍然是 C++ 类型。相反,您要做的是创建 Windows::Foundation::Collection::IVector&lt;T&gt; 类型的公共属性,该属性由 Platform::Collections::Vector&lt;T&gt; 类型的私有成员变量支持。

    基本上:

    public:
    property Windows::Foundation::Collections::IVector<float>^ num{
            Windows::Foundation::Collections::IVector<float>^ get(){
                return NUM;
            }
        }
    private:
    Platform::Collections::Vector<float>^ NUM;
    

    我避免提及属性设置器,因为这很棘手(您的私有类型也需要是 IVector,如果它来自 C++,它只会是 Platform::Collections::Vector)。

    【讨论】:

    • 非常感谢安迪。我设法通过 safe_casting setter 函数的值来做到这一点。如果不是因为你,我可能还会在键盘上砸我的头。
    【解决方案2】:

    您可以使用Windows::Foundation::Collections::IVector&lt;float&gt;^ 进行签名。由于手头没有编译器,我无法对此进行测试。

    【讨论】:

      【解决方案3】:

      嘿伙计们,我设法让这个工作,所以我想我会发布代码。头文件是这样的:

      public:
      property Windows::Foundation::Collections::IVector<int>^ num{
          void set(Windows::Foundation::Collections::IVector<int>^ e){
              NUM = safe_cast<Platform::Collections::Vector<int>^>(e);
          };
          Windows::Foundation::Collections::IVector<int>^ get(){
              return NUM;
          };
      };
      private:
      Platform::Collections::Vector<int>^ NUM;
      

      .cpp 文件中的代码是:

      num = ref new Vector<int>;  
      num->Append(5);
      num->Append(54);
      TextBox1->Text = num->GetAt(0).ToString() + "\n" + num->GetAt(1).ToString();
      

      结果会将值 5 和 54 写入文本框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        • 2019-10-23
        • 2021-09-17
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2018-08-19
        相关资源
        最近更新 更多