2011-05-29 51 views
0

任何人都可以幫到#C++ CLI語法:泛型方法?

我需要一個在C++/CLI中的通用方法。

我嘗試的時刻如下:

generic<K, ref class U> 
void OnUpdate (
    K key, 
    U update 
); 

可悲的是它不工作。該方法必須接受K和U,和C#的定義是:

void DataUpdate<K, U>(DataUpdate<K, U> update) where U : class; 

(是的,方法是不同的 - 的OnUpdate將檢查的aPoint到的接口是否已設定,則調用此方法在界面,像事件處理程序,所以參數必須匹配)。

C++/CLI中的通用語法無法迴避。定義K也是一個階級,我沒有問題。


更新:

由於第一幫助我處理的頭文件:

generic<typename K, typename U> 
where U : ref class 
[System::Security::SecuritySafeCritical()] 
void OnUpdate (
    K key, 
    U update 
); 

回答

4

這並不是說清楚你在尋找什麼。約束必須與其中關鍵字聲明:

generic<typename K, typename U> 
where U : ref class 
void OnUpdate (K key, U update) 
{ 
    // etc.. 
} 
+0

這幫助了我。我只是沒有正確地獲得hwole聲明。 – TomTom 2011-05-29 12:32:25

+0

這是一個聲明。 – 2011-05-29 12:34:55

+0

我知道。你的代碼幫助我理解;)現在它工作。 – TomTom 2011-05-29 12:35:35