在CArray中放的是自定义的类型,用GetAt()会出错,而用ElementAt()就行。
他们有什么区别呀?

解决方案 »

  1.   

    CArray::GetAt 
    TYPE GetAt( int nIndex ) const;Return ValueThe array element currently at this index.ParametersTYPETemplate parameter specifying the type of the array elements.nIndexAn integer index that is greater than or equal to 0 and less than or equal to the value returned by GetUpperBound.ResReturns the array element at the specified index.Note   Passing a negative value or a value greater than the value returned by GetUpperBound will result in a failed assertion.
    .............
    .............
    .............
    CArray::ElementAt 
    TYPE& ElementAt( int nIndex );Return ValueA reference to an array element.ParametersTYPETemplate parameter specifying the type of elements in the array.nIndexAn integer index that is greater than or equal to 0 and less than or equal to the value returned by GetUpperBound.ResReturns a temporary reference to the specified element within the array. It is used to implement the left-side assignment operator for arrays.
      

  2.   

    do u notice?  the two functions have different kinds of return value ....check ur code ...
      

  3.   

    GetAt返回对象,所以你的自定义对象要有copy constructor并且要overload你的"="操作符,如果没有重载,则编译器会声称缺省的。而ElementAt()返回的是对象的引用,可以直接操纵对象所以不会出错。
      

  4.   


    对呀俩函数返回类型就不一样呀,一个返回 index,一个返回引用
      

  5.   

    CArray ar;
    .......
    ar.GetAt(0) = 0;//出错, ar.SetAt(0, 0);
    ar.ElementAt(0) = 0; //OK,ElementAt取得的,可以作为左值(l value)
      

  6.   

    ruihuahan(飞不起来的笨鸟) 讲的非常正确
      

  7.   

    GetAt 返回对象的引用!
    后者只返回其值 了!