草庐IT

pimpl_ptr

全部标签

c++ - 模板中的 pimpl 成语;哪个智能指针?

我通常为pimpl使用boost::scoped_ptr(出于一个原因,因为如果我忘记处理复制构造函数,我不会感到惊讶)然而,对于模板,我不能只将析构函数放在完全定义了impl的cpp文件中,以满足scoped_ptr的析构函数的要求。无论如何它确实有效,但我不确定它是否保证有效或只是偶然。是否有一些“最佳实践”或标准?scoped_ptr是不可复制类中pimpls的最佳智能指针吗?templateclassC{public:C(){}~C(){}private:boost::scoped_ptrpimpl_;}; 最佳答案 碰巧H

c++ - 初始化 shared_ptr 成员变量,new vs make_shared?

当初始化一个shared_ptr成员变量时://.hclassCustomer{public:Customer();private:std::shared_ptrsomething_;}//.cppCustomer():something_(newOtherClass()){}对比Customer():something_(std::make_shared()){}是否允许使用make_shared版本?我似乎总是看到第一个版本,哪个是首选? 最佳答案 不允许make_shared的唯一时间是:如果您得到一个由其他人分配的裸指针并将

c++ - 这个 unique_ptr 的初始化有什么问题?

有人能告诉我,unique_ptr的以下初始化有什么问题吗?intmain(){unique_ptrpy(nullptr);py=newint;....}g++-O2xxx.cc-lm-oxxx-std=c++11说:error:nomatchfor‘operator=’(operandtypesare‘std::unique_ptr’and‘int*’)py=newint;^做unique_ptrpx(newint);工作得很好。 最佳答案 两段代码的初始化都很好,unique_ptr有constructors对于nullptr和

c++ - 为什么这个 auto_ptr 的 dynamic_cast 会失败?

#include"iostream"classA{private:inta;public:A():a(-1){}intgetA(){returna;}};classA;classB:publicA{private:intb;public:B():b(-1){}intgetB(){returnb;}};intmain(){std::auto_ptra=newA();std::auto_ptrb=dynamic_cast>(a);return0;}错误:不能dynamic_cast`(&a)->std::auto_ptr::get()const 最佳答案

c++ - 如何将 boost::shared_ptr 引入现有(大型)C++ 代码库?

我目前正尝试通过引入智能指针的使用来修复我们代码库中的一些弱点。代码库非常庞大,并且相互关联,就像一只喝过一对多咖啡的蜘蛛。我想知道以前是否有人尝试过,他们的方法是什么。我的第一步是typedef类,如下所示。#ifndefUSE_SMART_POINTERS#defineUSE_SMART_POINTERS0#endif#ifUSE_SMART_POINTERS==1#include#endifnamespaceProductX{//forwarddeclerationclassCTObject;//typedefs#ifUSE_SMART_POINTERS==1typedefboo

c++ - 如何实现 std::auto_ptr 是复制构造函数?

回到我的疯狂AutoArraythingy...(从那里引用重要的部分:classAutoArray{void*buffer;public://CreatesanewemptyAutoArrayAutoArray();//std::auto_ptrcopysemanticsAutoArray(AutoArray&);//Noteitcan'tbeconstbecausethe"other"reference//isnull'doncopy...AutoArray&operator=(AutoArray);~AutoArray();//Nothrowswap//Note:Atthemom

c++ - 为什么 vector.push_back(auto_ptr) 无法编译?

我了解到STL可以禁止程序员将auto_ptr放入容器中。例如下面的代码不会编译:auto_ptra(newint(10));vector>v;v.push_back(a);auto_ptr有拷贝构造函数,为什么这段代码还能通过? 最佳答案 查看thedefinitionofstd::auto_ptr:namespacestd{templatestructauto_ptr_ref{};templateclassauto_ptr{public:typedefXelement_type;//20.4.5.1construct/copy/

c++ - 什么时候将 unique_ptr 与 STL 容器一起使用才有意义? (C++11)

unique_ptr的容器似乎没有什么意义:你不能将它与初始化列表一起使用,而且我无法遍历容器(下面的解决方法)。我误会了什么吗?或者什么时候使用unique_ptr有意义和STL容器?#include#includeusingnamespacestd;structBase{voidgo(){}virtual~Base(){}};//virtual~Base()=default;gives//"declaredvirtualcannotbedefaultedintheclassbody"why?classDerived:publicBase{};intmain(){//vector>v

c++ - std::unique_ptr 编译器错误:派生类的成员无法访问基类的私有(private)成员

我得到CompilerErrorC2248当我尝试编译以下代码时:#include#includeusingnamespacestd;classdata{public:staticdataparse(){datad;data::parse(d);returnd;}list>l;private:staticvoidparse(data&node){}};intmain(){return0;}为什么?我该如何解决这个问题?注意:我使用std::shared_ptr而不是std::unique_ptr没有问题。 最佳答案 您需要为您的类型

c++ - 将对象转换为 std::unique_ptr

我有一个简单的菜鸟问题,我找不到答案:在C++中你如何转换一个普通对象inti;进入std::unique_ptr?std::unique_ptriptr=&i;//invalidstd::unique_ptriptr=static_cast>(&i);//invalid谢谢。 最佳答案 你不知道。delete无法删除该对象,这是unique_ptr将要执行的操作。你需要autoiptr=make_unique();在这里,我们将make_unique定义为一个与make_shared相同的效用函数,本来应该是Standard的,可