摘要: class base {}; class sub : public base {}; boost::shared_ptr bb(new sub()); base的析构函数并不是virtual的,但是,离开作用域后,将调用sub的析构函数而不是base的析构函数。 shared_ptr中的两个数据成员: T * px; // contained po... 阅读全文
posted @ 2013-01-12 18:04 avexer 阅读(176) 评论(0) 推荐(0) 编辑
摘要: 一、构造函数初始化列表的异常机制class demo{public: demo(int size) try : _array(new int[size]) { } catch (bad_alloc&) { }private: int *_array;};假设在分配内存时发生了异常,并且被我们catch到,但是,这个异常仍然会继续向外抛,以让调用者知道。可以这么说,编译器在每个catch块的最后又为我们加了一行throw;因此,安全起见,在构造这个对象的代码处,还需再写一次try,catch。try{ demo obj;}catch (bad_alloc&){}二、构造函数体中的 阅读全文
posted @ 2013-01-12 14:58 avexer 阅读(121) 评论(0) 推荐(0) 编辑
摘要: 有下面两个类:class base{};class sub : public base{};下面的异常代码:try{ base *bb = new sub(); throw(bb);}catch (sub*){ std::cout<<”this handler won’t execute”;}catch (base*){ std::cout<<”exxcute this handler”<<std::endl;}因此,最好不要throw指针类型的异常。 阅读全文
posted @ 2013-01-12 13:04 avexer 阅读(109) 评论(0) 推荐(0) 编辑