本文介绍了auto_ptr和PIMPL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 似乎使用auto_ptr<>在许多地方都不鼓励 赞成boost :: shared_ptr<> (或tr1 :: shared_ptr<>)。但是考虑一下 PIMPL习语,其中接口 对象和实现对象之间存在严格的1-1关系,并且实现对象持续 表示接口对象的生命周期。 ie //头文件 类WidgetImpl; class Widget { private: WidgetImpl * const pImpl; public: Widget() ; ~小工具(); // yada yada yada }; / / source file class WidgetInterface { private: friend class WidgetInterface; WidgetImpl( ){} ~WidgetImpl(){} // yada yada yada }; Widget :: Widget():pImpl(new WidgetImpl) { } Widget :: ~Widget( ) { 删除pImpl; } 在这种情况下,制作Widget有什么问题:: pImpl an auto _ptr< WidgetImpl> ;?在我看来,这会更安全,因为 很好。 解决方案 这里的三法则发生了什么? 小工具可以复制吗?复制分配怎么样?如果可以复制, 你知道auto_ptr有什么特别的复制吗?语义?实现的 所有权在这里表达不完整。请 正确并重新发布。 V It seems that the use of auto_ptr<> is discouraged in many places infavor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider aPIMPL idiom, where there is a strict 1-1 relationship between interfaceobjects and implementation object, and the implementation object lastsfor the lifetime of the interface object.i.e.// header fileclass WidgetImpl;class Widget {private:WidgetImpl* const pImpl;public:Widget();~Widget();// yada yada yada};// source fileclass WidgetInterface {private:friend class WidgetInterface;WidgetImpl() { }~WidgetImpl() { }// yada yada yada};Widget::Widget() : pImpl(new WidgetImpl){}Widget::~Widget(){delete pImpl;}In this case, is there any thing wrong with making Widget::pImpl anauto_ptr<WidgetImpl>? It seems to me that this would be a bit safer aswell. 解决方案What''s happened to the Rule of Three here? Can a ''Widget'' becopy-constructed? What about copy-assignment? If it can be copied,do you know what auto_ptr has rather peculiar "copy" semantics? Theownership of the implementation is expressed incompletely here. Pleasecorrect and re-post.Vauto_ptr would be fine as long as you disable or overload copying, butyou''d have to do that anyway. Assignment will automatically be disabledas you cannot assign auto_ptr.Beware of using boost::shared_ptr here. It might not behave the way youwant it to - because effectively it would make your widget a smart(shared) pointer to its implementation. If you copied your widget, bothwidgets would have a pointer to the SAME implementation. Maybe that''swhat you want - may be you want to be able to return your widget fromfunctions or have a collection of them. Effectively though your widgetwould be a wrapper for shared_ptr< Impl > and no more.(It''s very rare you''d want to clone widgets so my guess is that wouldbe the desired behaviour).If, for some reason, you did want to clone the widget then you couldeither implement that in your copy-constructor (and in operator= usingswap() ) or you could use a smart-pointer that clones (eitherautomatically or with copy-on-write).If you just want your widget to have move semantics and no more, youcan create a mover. (In future versions of C++ they will be part of thelanguage). A simple mover would have a pointer to the implementationand would snatch it from the widget (either snatch the pointer itselfor snatch the ownership - in this case you also have an ownershipflag). When you create a widget from a mover it receives the ownership(if the mover has it). I think that boost have created a unique_ptrthat can implement that as a template. 这篇关于auto_ptr和PIMPL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 14:39