ISO C++
1
}
執行結果
若使用C++/CLI,也是可以實現
1
}
執行結果
若用C#,以下代碼無法compile成功
1
using System;
2
3
}
2
3
C#無法compile成功
錯誤訊息為
Error 1 The call is ambiguous between the following methods or properties: 'Interface1.func1()' and 'Interface2.func1()' D:\__Clare\CSharp\CSharpLab\Class3.cs 43 5 CSharpLab
Error 2 The type 'Interface1' must be convertible to 'Interface2' in order to use it as parameter 'T' in the generic type or method 'Generic1<T>' D:\__Clare\CSharp\CSharpLab\Class3.cs 52 24 CSharpLab
Error 3 The type 'Interface2' must be convertible to 'Interface1' in order to use it as parameter 'T' in the generic type or method 'Generic1<T>' D:\__Clare\CSharp\CSharpLab\Class3.cs 55 15 CSharpLab
Error1我能理解為什麼不能過,不過並非我故意惡搞,因為實務上的確愈到這樣的需求...
Error2和Error3則真的無法理解了..
看來C#的泛型和C++泛型差異頗大...
請各位指點,該怎麼改才好,改了兩天想不出來...。
<06/16/2007>感謝hyifeng多次的指點,C#的Generics寫法如下
1
}
之前C#寫法,主要錯誤為