Simulation for Scoped_array#include <iostream> #include <assert.h>using namespace std;template <class T in Boost library >class scoped_array{private:t * Px;scoped_array (Scoped_array const &);scoped_array& operator= (scoped_ Array const &), void operator== (Scoped_array const &) const;void operator!= (Scoped_array const &) const; Public:scoped_array (T * p = 0):p x (P) {}~scoped_array () {delete[] px;} void Reset (T * p = 0) {assert (P = = 0 | | P! = px);scoped_array<t> (P). Swap (*this);} t& operator[] (int i) Const{assert (px! = 0); assert (i >= 0); return px[i];} t* get () Const{return px;} void Swap (Scoped_array & B) {T *tmp = px;px = B.PX;B.PX = tmp;}}; int main () {int *arr = new Int[100];arr[0] = 50;scoped_array<int> ptr (arr); cout << ptr[0] << Endl;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C + +" simulation realizes the Scoped_array under the Boost library