2

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^)。

4

1 に答える 1

3

gcnewは常にハンドル(^)を返します。
だからここにあなたが試すことができるものがあります。それが本当にあなたのニーズを満たしているかどうかわからない-

テンプレートrefクラスSuperClass{public: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();
于 2012-06-08T03:15:45.607 に答える