bool Create(int *a, int n ) { if ( m_pPtr ) return false; m_pPtr = new int[n]; for ( int i = 0; i < n; i++ ) { m_pPtr[i] = 3 / a[i]; } return true; } 合理:
template class auto_array { public: eXPlicit auto_array(_Ty *pPtr=0)throw():m_Ptr(pPtr){} ~auto_array(){delete[]m_Ptr;} void reset(_Ty *pPtr=0){if(pPtr!=m_Ptr){delete[]m_Ptr;m_Ptr=pPtr;}} _Ty* release(void){_Ty *pTemp=m_Ptr;m_Ptr=0;return pTemp;} private: auto_array(const auto_array&other){} auto_array& Operator=(const auto_array& other){} _Ty *m_Ptr; }; bool A::Create(int *a, int n ) { if ( m_pPtr ) return false; auto_array PtrGuard( new int[n] ); for ( int i = 0; i < n; i++ ) { if ( 0 == a[i] ) { return false; } PtrGuard .get()[i] = 3 / a[i]; } m_pPtr = PtrGuard.release(); return true; } 解析: