2012-06-08 106 views
2

什麼是適當的方式來獲得在下面的代碼non-handle類型:C++ CLI句柄類型

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

回答

3

gcnew總是返回句柄(^)。
所以這裏是你可以嘗試的東西。不知道是否真的滿足您的需求 -

模板引用類父類 { 市民: void方法(){ 類型 ^ X = gcnew類型( 「你好」); } };

SuperClass<String> superClass; 
superClass.Method(); 

template <typename Type> ref class SuperClass 
{ 
public: 
    void Method() 
    { 
    Type x = "Hello"; 
    } 
}; 

SuperClass<String^> superClass; 
superClass.Method(); 
+0

我最後的評論實際上告訴我不能避免傳遞**手柄式**作爲模板參數。但是,謝謝:) –

+0

請檢查我對代碼的編輯是否適合您。 – Superman

+0

它沒有:)但是,你把我推向了我的案例的正確解決方案,再次感謝。 –