包装数组

2018-05-31  本文已影响0人  ww4u
//  int* ZCP = (int*)malloc((length - 1) * sizeof(int));
//  memset(ZCP, 0, (length - 1) * sizeof(int));
//  double* scaZCP = (double*)malloc(joinNum *(length - 1) * sizeof(double));
//  memset(scaZCP, 0, joinNum * (length - 1) * sizeof(double));

        smartBuffer<int> ZCP( (length - 1) );
        smartBuffer<double> scaZCP( joinNum *(length - 1) );
template<typename T>
class smartBuffer{
public:
    T *m_pBuffer;
    int mSize;

public:
    smartBuffer( int size )
    {
        m_pBuffer = NULL;
        mSize = 0;

        if ( size > 0 )
        {
            allocate( size );
        }
    }

    ~smartBuffer()
    {
        if ( mSize != 0 )
        {
            delete []m_pBuffer;
            m_pBuffer = NULL;
            mSize = 0;
        }
    }

    void allocate( int n )
    {
        m_pBuffer = new T[ n ];
        mSize = n;
        Q_ASSERT( NULL != m_pBuffer );

        memset( m_pBuffer, 0, sizeof(T)*mSize );
    }

    T& operator[]( int i )
    {
        Q_ASSERT( i >=0 && i < mSize );
        return m_pBuffer[i];
    }
};
上一篇下一篇

猜你喜欢

热点阅读