如何从IVector ^转换为Vector ^?文档涵盖了隐式工作的反向情况,但是当我尝试隐式转换和编译时,这个不会编译,但是当我尝试编译它时会引发异常。我已经查看了参考资料,但我无法理解这一点。
这样做的原因是我希望将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的一些问题,所以我尝试使用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;
}
}
};
答案 0 :(得分:1)
这通常是通过safe_cast<T>
:
Vector^ asVector = safe_cast<Vector^>(theIVectorHandle);
有关详细信息,请参阅MSDN article on Casting in C++/CX。