草庐IT

c++ - 表达式 "(ptr == 0) != (ptr == (void*)0)"真的可以吗?

我在aforumthread中阅读了此声明链接到inacommentby@jsantander:Keepinmindthatwhenyouassignorcompareapointertozero,thereissomespecialmagicthatoccursbehindthescenestousethecorrectpatternforthegivenpointer(whichmaynotactuallybezero).Thisisoneofthereasonswhythingslike#defineNULL(void*)0areevil–ifyoucompareachar*to

c++ - 在 C++11 中将对象的所有权从一个 unique_ptr 转移到另一个 unique_ptr?

在C++11中,我们可以使用std::move()将一个对象的所有权转移到另一个unique_ptr。所有权转移后,让出所有权的智能指针变为null,get()返回nullptr.std::unique_ptrp1(newint(42));std::unique_ptrp2=std::move(p1);//Transferownership在将所有权转移到另一个unique_ptr时,这在哪些情况下有用? 最佳答案 以下情况涉及从一个unique_ptr转移所有权另一个:从函数返回,并作为参数传递给构造函数等函数。假设你有一些多态类

c++ - 在 C++11 中将对象的所有权从一个 unique_ptr 转移到另一个 unique_ptr?

在C++11中,我们可以使用std::move()将一个对象的所有权转移到另一个unique_ptr。所有权转移后,让出所有权的智能指针变为null,get()返回nullptr.std::unique_ptrp1(newint(42));std::unique_ptrp2=std::move(p1);//Transferownership在将所有权转移到另一个unique_ptr时,这在哪些情况下有用? 最佳答案 以下情况涉及从一个unique_ptr转移所有权另一个:从函数返回,并作为参数传递给构造函数等函数。假设你有一些多态类

c++ - c++11 中的 intrusive_ptr

C++11是否有与boost::intrusive_ptr等价的东西?我的问题是我的C++代码有一个C风格的界面。接口(interface)的两端都可以使用C++,但出于兼容性原因,需要公开C接口(interface)。我不能使用std::shared_ptr因为我必须通过两个(或更多)智能指针来管理对象。我无法用boost::intrusive_ptr之类的方法找到解决方案。 最佳答案 Doesc++11havesomethingequivalenttoboost::intrusive_ptr?没有。它确实有std::make_s

c++ - c++11 中的 intrusive_ptr

C++11是否有与boost::intrusive_ptr等价的东西?我的问题是我的C++代码有一个C风格的界面。接口(interface)的两端都可以使用C++,但出于兼容性原因,需要公开C接口(interface)。我不能使用std::shared_ptr因为我必须通过两个(或更多)智能指针来管理对象。我无法用boost::intrusive_ptr之类的方法找到解决方案。 最佳答案 Doesc++11havesomethingequivalenttoboost::intrusive_ptr?没有。它确实有std::make_s

c++ - 如何在构造函数中初始化 std::unique_ptr?

A.hpp:classA{private:std::unique_ptrfile;public:A(std::stringfilename);};A.cpp:A::A(std::stringfilename){this->file(newstd::ifstream(filename.c_str()));}我得到的错误被抛出:A.cpp:7:43:error:nomatchforcallto‘(std::unique_ptr>)(std::ifstream*)’有没有人知道为什么会发生这种情况?我尝试了许多不同的方法来让它工作,但都无济于事。 最佳答案

c++ - 如何在构造函数中初始化 std::unique_ptr?

A.hpp:classA{private:std::unique_ptrfile;public:A(std::stringfilename);};A.cpp:A::A(std::stringfilename){this->file(newstd::ifstream(filename.c_str()));}我得到的错误被抛出:A.cpp:7:43:error:nomatchforcallto‘(std::unique_ptr>)(std::ifstream*)’有没有人知道为什么会发生这种情况?我尝试了许多不同的方法来让它工作,但都无济于事。 最佳答案

c++ - shared_ptr 和 weak_ptr 失败的小例子

我在使用shared_ptr时遇到问题和weak_ptr连同enable_shared_from_this.当我用谷歌搜索我所看到的症状时,每个人都建议“当没有shared_from_this()实例拥有你的对象时,你不能使用shared_ptr。但这不是我的情况。考虑这段代码:#include#includeclassMyClass:std::enable_shared_from_this{public:voidthis_fails(){//Doesn'tevenassert(),becauseitthrowsbad_weak_ptrassert(shared_from_this()

c++ - shared_ptr 和 weak_ptr 失败的小例子

我在使用shared_ptr时遇到问题和weak_ptr连同enable_shared_from_this.当我用谷歌搜索我所看到的症状时,每个人都建议“当没有shared_from_this()实例拥有你的对象时,你不能使用shared_ptr。但这不是我的情况。考虑这段代码:#include#includeclassMyClass:std::enable_shared_from_this{public:voidthis_fails(){//Doesn'tevenassert(),becauseitthrowsbad_weak_ptrassert(shared_from_this()

c++ - 数组的 auto_ptr

简而言之,我想知道数组是否有类似auto_ptr的类型。我知道我可以自己推出,我只是确保那里没有什么东西。我也知道vector。但是我不认为我可以使用它们。我正在使用几个WindowsAPI/SDK,例如WindowsMediaSDK、DirectShowAPI,它们是为了取回一些结构来调用一个函数,该函数需要一个指针和一个大小两次。第一次传递NULL作为指针来取回我必须分配的结构的大小,以便接收我正在寻找的数据。例如:CComQIPtrpProps(m_pStreamConfig);DWORDcbType=0;WM_MEDIA_TYPE*pType=NULL;hr=pProps->G