->, , set_controller . , , , . , , . 13.10  . , . , , , ? : f() , ? : ? , , . : "" " , ". , , , , , , , . , , , , . . , , , . , , . ++ , . ++ - . ++. operator new() , operator delete() - : inline size_t align(size_t s) /* , */ { union Word { void* p; long double d; long l; } int x = s + sizeof(Word) - 1; x -= x%sizeof(Word); return x; } static void* freep; // start void* operator new(size_t s) // { void* p = freep; s = align(s); freep += s; return p; } void operator delete(void*) { } // , . , , , , , . , , . 13.10.1  . , : , ? - , , , . , delete ( - , ). , , ? , , - . , " " . " ", , . , , . (. .) , . , , . . , "". , . ( ) . , . : , , , ( ) , - . ($$8.8): class Register { Map<void*, void (*) (void*)> m; public: insert(void* po, void(*pf)()) { m[po]=pf; } remove(void* po) { m.remove(po); } }; Register cleanup_register; , , : class X { // ... static void cleanup(void*); public: X() { cleanup_register.insert(this,&cleanup); // ... } ~X() { cleanup(this); } // ... }; void X::cleanup(void* pv) { X* px = (X*)pv; cleanup_register.remove(pv); // } Register , - void*. 13.10.2  , . , , Vector? , int, , . , , , Shape, . . : template<class T> Vector { T* p; int sz; public: Vector(int s) { p = new T[sz=s]; } // ... }; Shape, . Vector<Shape*> vsp(200); // Vector<Shape> vs(200); // , Shape. , , , . : void f() // // { Vector<Shape*> v(10); Circle* cp = new Circle; v[0] = cp; v[1] = new Triangle; Square s; v[2] = &s; delete cp; // , // , } Vector $$1.4.3, Triangle ( ), . - . : void g() // { Vector<Shape*> v(10); Circle* cp = new Circle; v[0] = cp; v[1] = new Triangle; Square s; v[2] = &s; delete cp; delete v[1]; } , : template<class T> MVector { T* p; int sz; public: MVector(int s); ~MVector(); // ... }; template<class T> MVector<T>::MVector(int s) { // s p = new T[sz=s]; for (int i = 0; i<s; i++) p[i] = 0; } template<class T> MVector<T>::~MVector() { for (int i = 0; i<s; i++) delete p[i]; delete p; } , MVector . , MVector , . MVector , : void h() // { MVector<Shape*> v(10); Circle* cp = new circle(); v[0] = cp; v[1] = new Triangle; Square s; v[2] = &s; v[2] = 0; // s // // } , , , Map ($$8.8), , . MVector, , , , . , , . , , (, ?): template<class T> MVector { // ... private: MVector(const MVector&); // MVector& operator=(const MVector&); // // ... }; , (, , ), . , . , 100 , , , 1000 , 1100 . , , , , . , 100 000, , 100. , , , , ? , ? , . . (Vector MVector), , , : template<class T> PVector { T** p; int sz; int managed; public: PVector(int s, int managed = 0 ); ~PVector(); // ... }; template<class T> PVector<T>::PVector(int s, int m) { // s p = new T*[sz=s]; if (managed = m) for (int i = 0; i<s; i++) p[i] = 0; } template<class T> PVector<T>::~PVector() { if (managed) { for (int i = 0; i<s; i++) delete p[i]; } delete p; } , , $$13.9. , , , . . , . , : template<class T> class Handle { T* rep; int* pcount; public: T* operator->() { return rep; } Handle(const T* pp) : rep(pp), pcount(new int) { (*pcount) = 0; } Handle(const Handle& r) : rep(r.rep), pcount(r.count) { (*pcount)++; } void bind(const Handle& r) { if (rep == r.rep) return; if (--(*pcount) == 0) { delete rep; delete pcount; } rep = r.rep; pcount = r.pcount; (*pcount)++; } Handle& operator=(const Handle& r) { bind(r); return *this; } ~Handle() { if (--(*pcount) == 0) { delete rep; delete pcount; } } }; 13.10.3  . , , . "" . , , . , , , , . $$5.5.6 $$6.7 X::operator new() X::operator delete() X. . X Y , . , , , . , : class X { static Pool my_pool; // ... public: // ... void* operator new(size_t) { return my_pool.alloc(); } void operator delete(void* p) { my_pool.free(p); } }; Pool X::my_pool(sizeof(X)); Pool . my_pool sizeof(X). X Pool . , "", X::operator new() . X::operator delete() . Y X, sizeof(Y)>sizeof(X), Y . X . , Y . Pool . . , . PooL, . Pool: class Pool { struct Link { Link* next; } const unsigned esize; Link* head; Pool(Pool&); // void operator= (Pool&); // void grow(); public: Pool(unsigned n); ~Pool(); void* alloc(); void free(void* b); }; inline void* Pool::alloc() { if (head==0) grow(); Link* p = head; head = p->next; return p; } inline void Pool::free(void* b) { Link* p = (Link*) b; p->next = head; head = p; } Pool.h. . Pool : Pool::Pool(unsigned sz) : esize(sz) { head = 0; } Pool::grow() head, . - 5 6 $$13.11. void Pool::grow() { const int overhead = 12; const int chunk_size = 8*1024-overhead; const int nelem = (chunk_size-esize)/esize; char* start = new char[chunk_size]; char* last = &start[(nelem-1)*esize]; for (char* p = start; p<last; p+=esize) ((Link*)p)->next = ((Link*)p)+1; ((Link*)last)->next = 0; head = (Link*)start; } 13.11  1. (*3) - Type_info. 2. (*3) Type_info, Type_info::get_info() , - Type_info. 3. (*2.5) Dialog_box, ( )? , ? 4. (*4) . , : , , , . ? ? ? ( )? ? 5. (*3) Pool $$13.10.3. , . 6. (*2.5) Pool , . , Pool char, . 7. (*3) ++ , . ? , $$13.5 . 8. (*2.5) , , , . , , , , $$12.2.7.1. ? 9. (*4) Storable writeout() readin(). , . Storable . . 10.(*4) Persistent save() nosave(), , . ? Persistent . Persistent , ? . 11.(*3) stack, create() ( ), delete() ( ), push() ( ) pop() ( ). . id. , stack::id, . stack $$8.2. 12.(*3) stack, ($$13.3). , stack. , . , , $$8.2. 13.(*3) stack, . : " , ". 14.(*3.5) Oper, ( ) ( ). cat_object, Oper void*. cat_object : add_oper(), ; remove_oper(id), Oper c id; operator() (id,arg), Oper c id. cat_object Oper. , . 15.(*3) Object, cat_object. Object String. , . 16.(*3) Object Class, . , . 17.(*3) Stack, , Object. , , . , .