C++编程(C)

Educn 14631 unexpectedhandler

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 件x和锁y,这些请求都 可能失败并抛出异常,若普通 设计,程序员需要写大量代码 为此,可以使用两个类(子对 象)来实现该类,这使得程序 员根本不必去考虑任何异常情 况 若在创建了aa但还没有bb的情 况下出现了异常,aa的析构函 数会被调用,bb的则不会 Beijing University of Posts & Telecommunications Beijing [email protected] 示例 class Y{ int* p; void init(); public: Y(int s) { p=new int(s); init(); } ~Y(){delete p;} //... }; class Z{ vector<int> p; void init(); public: Z(int s) : p(s) { init(); } // ... }; // 改进后的代码 // 一段有问题的代码,可能 导致存储流失 Beijing University of Posts & Telecommunications Beijing [email protected] 14.4.2 auto_ptr 标准库提供的auto_ptr支持“资源申请即初始化”的 技术 下例中,Rectangle和由pc所指向的Circle都将被 删除,无论是否有异常被抛出 void f(Point p1, Point p2, auto_ptr<Circle> pc, Shape* pb) { auto_ptr<Shape> p(new Rectangle(p1,p2)); /...
View Full Document

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online