在以下代码中获取non-handle
类型的正确方法是什么:
template <typename Type> ref class SuperClass
{
public:
void Method()
{
Type x = gcnew ???? (...);
// I want it to be instantiated into 'String^ x = gcnew String(...).
// Is there a way to "dereference" the handle type in C++ \ CLI ?
}
};
SuperClass<String^> superClass;
superClass.Method(); // <---- Won't compile
此外,使用句柄类型作为模板参数是强制性的(这是更大示例的一部分,我不能简单地将模板类型更改为String
而不是 String^
)。
最佳答案
gcnew 始终返回句柄 (^)。
所以你可以尝试一下。不确定它是否真的满足您的需求 -
<罢工> 模板引用类父类(super class) { 民众: 无效方法() { Type^ x = gcnew Type("Hello"); } };罢工>
<罢工>SuperClass<String> superClass;
superClass.Method();
罢工><罢工>罢工>
template <typename Type> ref class SuperClass
{
public:
void Method()
{
Type x = "Hello";
}
};
SuperClass<String^> superClass;
superClass.Method();
关于c# - C++ CLI 句柄类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10942559/