generic<typename T> where T:IComparable
T MaxElement(array<T>^ x)
{
T max = x[0];
for(int i = 1; i < x->Length; i++)
if(max->CompareTo(x[i]) < 0)
max = x[i];
return max;
}
max->CompareTo   为什么不是max.CompareTo
 

解决方案 »

  1.   

    c++.net ?
    我说怎么那么古怪呢.. ..array<T>^ x 这表示引用吧?引用自然是要用-> 
      

  2.   

    单纯C++中当左侧为对象时用.  左侧为指针是用->
      

  3.   

    如果按你的理解,也只能说x是指针(引用),x[0]可未必是。从字面上看,x[0]的类型是T,而不是T^,这正是楼主的困惑所在由于c#没有指针,统统只有点,而.net基本上是为c#设计的。“翻译”为c++ cli代码时,要看实际上是不是refMSDN有云:Whereas in other Visual Studio languages, you can use the dot operator to access a member of a managed class, a pointer to the object in C++ means you have to use the -> operator to access the memberarray是ref class,所以其元素的成员需要用指针访问
      

  4.   

    更正:关于array<>和ref,我的理解是错的,同求正确答案
      

  5.   

    [Quote=引用 6 楼 yisikaipu 的回复:]
    更正:关于array<>和ref,我的理解是错的,同求正确答案array<> 到底是不是ref class??但是4楼前面说的都很有道理。