atl里面的代码,不知道这个operator[]的作用是个啥?

解决方案 »

  1.   

    对coust写错表示歉意。
    这是类中的两个函数,怎么用呢,能不能给个例子啊?
      

  2.   

    假如定义了一个该类的对象,名为test,就是用test[0]、test[1]这样的写法,执行到这里时会调用这个函数。
      

  3.   

    class CSimpleArray
    {
    public:
    // Construction/destruction
    ...
    // Operations
    ...
    const T& operator[] (int nIndex) const
    {
    ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
    if(nIndex < 0 || nIndex >= m_nSize)
    {
    _AtlRaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
    }
    return m_aT[nIndex];
    }
    T& operator[] (int nIndex)
    {
    ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
    if(nIndex < 0 || nIndex >= m_nSize)
    {
    _AtlRaiseException(EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
    }
    return m_aT[nIndex];
    }
    ...
    // Implementation
    class Wrapper
    {
    public:
    Wrapper(const T& _t) : t(_t)
    {
    }
    template <class _Ty>
    void * __cdecl operator new(size_t, _Ty* p)
    {
    return p;
    }
    template <class _Ty>
    void __cdecl operator delete(void* /* pv */, _Ty* /* p */)
    {
    }
    T t;
    };
    // Implementation
    void InternalSetAtIndex(int nIndex, const T& t)
    {
    new(m_aT + nIndex) Wrapper(t);
    } typedef T _ArrayElementType;
    T* m_aT;
    int m_nSize;
    int m_nAllocSize;
    };
    不好意思,还是不太明白,
    我是在这里看到的代码,按您的说法,test[0]是不是就表示test.m_aT[0],可m_aT是T类指针,test.m_aT[0]又是什么意思啊
    另外,顺便问下,new(m_aT + nIndex) Wrapper(t)又应该怎么理解啊,初学MFC,感觉好复杂,请多多指教:)