2012-02-03 38 views
1

的C#示例通用/模板類在C#:如何定義像下面

public sealed class StateMachine<TState, TTrigger> 

我想寫一個C++等價物。

+0

您將要重新說明 – trickdev 2012-02-03 13:23:05

回答

3

像這樣:

template <typename TState, typename TTrigger> 
class StateMachine 
{ 
    //... 
}; 
0

你可以使用什麼斯圖爾特和XEO建議,如果你想在這裏做密封類是link,解釋如何做到這一點。

編輯:this更好。

1

This site對如何製作模板類有很好的解釋。例如:

// function template 
#include <iostream> 
using namespace std; 

template <class T> 
T GetMax (T a, T b) { 
    T result; 
    result = (a>b)? a : b; 
    return (result); 
} 

int main() { 
    int i=5, j=6, k; 
    long l=10, m=5, n; 
    k=GetMax<int>(i,j); 
    n=GetMax<long>(l,m); 
    cout << k << endl; 
    cout << n << endl; 
    return 0; 
} 

使用此結合this previous Stack Overflow question實現類的密封方面。