Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
318
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

os << "--------" << endl; return os;

}

void Bicycle::print(vector<Bicycle*>& vb, ostream& os) {

copy(vb.begin(), vb.end(), ostream_iterator<Bicycle*>(os, "\n"));

cout << "--------" << endl; } ///:~

Here’s a test:

//: C09:BikeTest.cpp //{L} Bicycle #include "Bicycle.h" #include <algorithm> using namespace std;

int main() { vector<Bicycle*> bikes; BicycleGenerator bg;

generate_n(back_inserter(bikes), 12, bg); Bicycle::print(bikes);

} ///:~

Exercises

1.Create a heap compactor for all dynamic memory in a particular program. This will require that you control how objects are dynamically created and used (do you overload operator new or does that approach work?). The typically heap-compaction scheme requires that all pointers are doublyindirected (that is, pointers to pointers) so the “middle tier” pointer can be manipulated during compaction. Consider overloading operator-> to accomplish this, since that operator has special behavior which will probably benefit your heap-compaction scheme. Write a program to test your heap-compaction scheme.

Chapter 16: Design Patterns

428

Соседние файлы в предмете Программирование