【问题标题】:C++/CX: Converting from IVector<T>^ to Vector<T>^C++/CX:从 IVector<T>^ 转换为 Vector<T>^
【发布时间】:2013-06-05 16:32:37
【问题描述】:

如何将 IVector^ 转换为 Vector^?文档涵盖了隐式工作的相反情况,但是当我尝试隐式转换和编译时这个不会编译,但是当我尝试转换它时会引发异常。我已经查看了参考资料,但无法涵盖。

这样做的原因是我希望将 Vector^ 存储在我的类中,并且仅在与 JavaScript 通信的属性中使用 IVector,因为 Vector 工作得更快,应该用于内部计算。

谢谢

编辑: 我为太简短而道歉。这是对这种情况的更广泛解释。

if(parent->childNodes != nullptr && parent->childNodes->Size > 0)
    {
        for (uint32 i = 0; i < parent->childNodes->Size; i++)
        {
            ContentElementsNode^ childNode;
            IVector<ContentElementsNode^>^ childNodes = parent->childNodes;
            childNode = childNodes->GetAt(i);

            Vector<ContentElementsNode^>^ childWords = wordObjects(childNode);
            for each (ContentElementsNode^ word in childWords)
                result->Append(word);
        }
    }

函数在第一次调用 GetAt(i) 行时失败。我认为这可能是 IVector 的一些问题,所以我尝试使用 Vector 但无法转换它。这是 ContentElementsNode 声明:

public ref class ContentElementsNode sealed
{

private:
    IVector<ContentElementsNode^>^ childNodes_;
    String^ textContent_;
    String^ localName_;
    Rectangle boundingBox_;
    String^ id_;
public:
    ContentElementsNode(void);
    property IVector<ContentElementsNode^>^ childNodes {
        void set(IVector<ContentElementsNode^>^ value)
        {
            childNodes_ = value;
        }
        IVector<ContentElementsNode^>^ get()
        {
            return childNodes_;
        }
    }
    property String^ textContent {
        String^ get() {
            return textContent_;
        }
        void set(String^ value) {
            textContent_ = value;
        }
    }
    property String^ localName {
        String^ get() {
            return localName_;
        }
        void set(String^ value) {
            localName_ = value;
        }
    }
    property Rectangle boundingBox {
        Rectangle get() {
            return boundingBox_;
        }
        void set(Rectangle value) {
            boundingBox_ = value;
        }
    }
    property String^ id {
        String^ get() {
            return id_;
        }
        void set(String^ value) {
            id_ = value;
        }
    }

};

【问题讨论】:

  • 函数调用不应在 GetAt() 行失败。你在什么时候用集合初始化后备存储(childNodes_)?
  • 前端部分好像有错误,所以通过ABI传递了无效值。事实证明,该错误没有任何教育目的,我认为应该删除整个线程?很抱歉给您带来麻烦。

标签: c++ vector type-conversion c++-cx


【解决方案1】:

这通常通过safe_cast&lt;T&gt;:

完成
Vector^ asVector = safe_cast<Vector^>(theIVectorHandle);

详情请见MSDN article on Casting in C++/CX

【讨论】:

  • 谢谢,不知道怎么错过了。
  • 我的错,它实际上不起作用,我在测试时打错了。 Vector&lt;ContentElementsNode^&gt;^ childNodes = safe_cast&lt;Vector&lt;ContentElementsNode^&gt;^&gt;(parent-&gt;childNodes); 我是这样称呼它的。
  • @DamjanDakic 你为什么要做这个演员?为什么不直接使用IVector&lt;T&gt;^?话虽如此 - 如果您需要更多帮助,您应该显示更多代码,包括 childNodes 的声明...
  • 抱歉,我编辑了这个问题,希望现在已经足够解释了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2016-03-31
  • 2020-10-17
  • 2019-10-20
  • 2020-05-07
  • 1970-01-01
相关资源
最近更新 更多