在以下代码中获取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总是返回一个句柄(^)。
因此,您可以尝试以下方法。不知道它是否真的满足您的需求-

模板ref类SuperClass
{
上市:
void Method()
{
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();

10-05 23:46
查看更多