e) throw(Shape::BadShapeCreati

Автор admin Суббота, 20 июня 2009 11:32

e) throw(Shape::BadShapeCreation) { if(type == “Circle”) Фабрика 505 s * new Circle; else if(type -= “Square”) s – new Square: else throw BadShapeCreation(type): drawO: // Виртуальный вызов в конструкторе } char* sl[] = { “Circle”. “Square”. “Square”. “Circle”. “Circle”. “Circle”. “Square” }: int mainO { vector shapes; cout « “virtual constructor calls:” « endl: try { for(size_t i = 0; i < sizeof si / sizeof sl[0]: i++) shapes.push_back(new Shape(sl[i])); } catch(Shape::BadShapeCreation e) { cout « e.whatО « endl; purge(shapes); return EXJ.T_FAJ.LURE: } for(size_t i = 0; i < shapes.sizeO; i++) { shapes[i]->draw(); cout « “test” « endl: shapes[i]->test(); cout « “end test” « endl; shapes[i]->erase(): } Shape c(“Circle”); // Создание в стеке cout « “destructor calls:” « endl; purge(shapes): } ///:- Базовый класс Shape хранит указатель на объект типа Shape в своей единственной переменной (при моделировании «виртуального конструктора» следует особенно внимательно следить за тем, чтобы этот указатель всегда инициализировался существующим объектом). Фактически базовый класс является посредником, по


Комментарии закрыты.