C++/CX:从 IVector<T>^ 转换为矢量<T>^

C++/CX: Converting from IVector<T>^ to Vector<T>^

本文关键字:gt lt 转换 CX C++ IVector      更新时间:2023-10-16

如何从矢量^转换到矢量^?文档涵盖了隐式工作的相反情况,但是当我尝试隐式转换和编译时,它不会编译,但当我尝试转换它时会引发异常。我已经看过参考资料,但是我无法涵盖这一点。

这样做的原因是我希望将Vector^存储在我的类中,并且仅在与JavaScript通信的属性中使用Vector,因为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)行时失败。我认为这可能是一些问题与矢量,所以我尝试使用矢量代替,但无法cast它。下面是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;
        }
    }
};

这通常通过safe_cast<T>:

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

有关详细信息,请参阅MSDN关于c++/CX中的强制转换的文章。