// Phil Ottewell's STL Course - http://www.pottsoft.com/home/stl/stl.htmlx // // Example 2.2 © Phil Ottewell 1997 // // Purpose: // Demonstrate simple class template // ANSI C Headers #include // C++ STL Headers #include template class MyVector { public: MyVector() { obj_list = new T[ size ]; nused = 0; max_size = size; } ~MyVector() { delete [] obj_list; } void Append( const T &new_T ) { if ( nused < max_size ) obj_list[nused++] = new_T; } T &operator[]( int ndx ) { if ( ndx < 0 || ndx >= nused ) { throw("up"); // barf on error } return( obj_list[ndx] ); } private: int max_size; int nused; T* obj_list; }; #ifdef _WIN32 using namespace std; #endif int main( int argc, char *argv[] ) { int i; const int max_elements = 10; MyVector< int, max_elements > phils_list; // End of declarations ... // Populate the list for ( i = 0; i < max_elements; i++) phils_list.Append( i ); // Print out the list for ( i = 0; i < max_elements; i++) cout << phils_list[i] << " "; cout << endl; return( EXIT_SUCCESS ); }