我开始研究C++ 11的智能指针,我没有看到任何有用的用法std::weak_ptr
.有人能告诉我什么时候std::weak_ptr
有用/必要吗?
好吧,所以我最后一次以C++为生,std::auto_ptr
所有的std lib都可用,而且boost::shared_ptr
风靡一时.我从未真正研究过提供的其他智能指针类型.我知道C++ 11现在提供了一些类型的提升,但不是全部.
那么有人有一个简单的算法来确定何时使用哪个智能指针?优选地包括关于哑指针(诸如原始指针T*
)和其他增强智能指针的建议.(像这样的东西会很棒).
std::unique_ptr
支持数组,例如:
std::unique_ptr<int[]> p(new int[10]);
Run Code Online (Sandbox Code Playgroud)
但它需要吗?可能使用std::vector
或更方便std::array
.
你觉得这个结构有用吗?
以下指针集之间有什么区别?什么时候在生产代码中使用每个指针,如果有的话?
例子将不胜感激!
scoped_ptr
shared_ptr
weak_ptr
intrusive_ptr
你在生产代码中使用boost吗?
这个程序有什么问题?
#include <memory>
#include <vector>
int main()
{
std::vector<std::unique_ptr<int>> vec;
int x(1);
std::unique_ptr<int> ptr2x(&x);
vec.push_back(ptr2x); //This tiny command has a vicious error.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48,
from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/memory:64,
from main.cpp:6:
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::unique_ptr<int>, _Tp* = std::unique_ptr<int>*]':
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:745:6: instantiated from 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>, _Alloc = std::allocator<std::unique_ptr<int> >, value_type = std::unique_ptr<int>]'
main.cpp:16:21: instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const …
Run Code Online (Sandbox Code Playgroud) 随着新标准的出现(以及某些编译器中已有的部件),新类型std::unique_ptr
应该是替代品std::auto_ptr
.
它们的用法是否完全重叠(因此我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做))或者我应该注意一些在阅读文档时不明显的差异?
此外,如果它是一个直接替代品(为什么给它一个新的名称),而不仅仅是改善std::auto_ptr
.
我在一些文章中读到,几乎从不使用原始指针.相反,它们应始终包含在智能指针内,无论是作用域还是共享指针.
但是,我注意到像Qt,wxWidgets和像Boost这样的库这样的框架永远不会返回也不会期望智能指针,就好像它们根本就没有使用它们一样.相反,他们返回或期望原始指针.有什么理由吗?当我编写公共API时,我应该远离智能指针,为什么?
只是想知道为什么许多重大项目似乎避免使用智能指针.
我无法理解智能指针在C++ 11中作为类成员的用法.我已经阅读了很多关于智能指针的内容,我想我确实理解了如何unique_ptr
和shared_ptr
/ weak_ptr
一般的工作.我不明白的是真正的用法.似乎每个人都建议使用unique_ptr
几乎所有的时间.但是,我将如何实现这样的事情:
class Device {
};
class Settings {
Device *device;
public:
Settings(Device *device) {
this->device = device;
}
Device *getDevice() {
return device;
}
};
int main() {
Device *device = new Device();
Settings settings(device);
// ...
Device *myDevice = settings.getDevice();
// do something with myDevice...
}
Run Code Online (Sandbox Code Playgroud)
假设我想用智能指针替换指针.A unique_ptr
不会起作用getDevice()
,对吧?那是我使用的时间shared_ptr
和weak_ptr
?没办法用unique_ptr
?对我来说似乎对大多数情况shared_ptr
更有意义,除非我在一个非常小的范围内使用指针?
class Device {
};
class Settings {
std::shared_ptr<Device> device;
public:
Settings(std::shared_ptr<Device> …
Run Code Online (Sandbox Code Playgroud) c++ ×10
smart-pointers ×10
c++11 ×7
pointers ×3
unique-ptr ×3
c++-faq ×2
shared-ptr ×2
api ×1
auto-ptr ×1
boost ×1
raii ×1
stl ×1
weak-ptr ×1