2013-06-05 39 views
1

如何將IVector ^轉換爲Vector ^?文檔涵蓋了隱式工作的相反情況,但是當我嘗試隱式轉換和編譯時,這個不會編譯,但是當我嘗試轉換它時引發異常。我已經看過參考書,但是我不能涵蓋這個。C++/CX:從IVector <T> ^轉換爲Vector <T>^

原因是我希望將Vector ^存儲在我的類中,並且僅在屬性中使用IVector才能與JavaScript進行通信,因爲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的一些問題,所以我試圖使用矢量,而不能施放它。這裏的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; 
     } 
    } 

}; 
+0

該函數調用不應該失敗在GetAt()行。您在什麼時候使用集合初始化後備存儲(childNodes_)? –

+0

看起來前端部分有錯誤,所以無效值通過ABI傳遞。事實證明,這個錯誤沒有任何教育意義,因此我認爲整個線程應該被刪除?抱歉,添麻煩了。 –

回答

1

這通常通過safe_cast<T>來完成:

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

有關詳細信息,請參閱MSDN article on Casting in C++/CX

+0

謝謝,我不知道我是如何錯過它的。 –

+0

我的不好,它實際上不起作用,我在測試時犯了一個錯字。 'Vector ^childNodes = safe_cast ^>(parent-> childNodes);' 以下是我稱之爲的方式。 –

+0

@DamjanDakic你爲什麼試圖做這個演員?爲什麼不直接使用'IVector ^'?這就是說 - 如果你需要更多的幫助,你應該顯示更多的代碼,包括'childNodes'的聲明... –

相關問題