pau*_*ulm 1 c++ shared-ptr c++11
所以我有一些代码:
class Thing
{
public:
Thing() = default;
};
class DbOfThings
{
public:
DbOfThings() = default;
std::weak_ptr<Thing> GetEntry(int someKey) const
{
// returns a weak_ptr from mThings, or null if a Thing
// that has someKey was not found
return std::weak_ptr<Thing>(nullptr);
}
private
// Idea is that this object owns these things, but other objects can grab a thing from here - but the thing can be killed off at any time
std::vector<std::share_ptr<Thing>> mThings;
Run Code Online (Sandbox Code Playgroud)
但这无法编译:
参数1从'std :: nullptr_t'到'const std :: weak_ptr&'没有已知的转换
为什么?我的approch是否允许其他对象持有另一个错误所拥有的东西?对于我来说,这似乎是weak_ptr的有效用例.我怎样才能做到这一点?
weak_ptr没有任何构造函数是采取nullptr_t或生指针,所以你不能构造一个与nullptr作为参数.获取一个空的weak_ptr只是默认构造一个.
std::weak_ptr<Thing> GetEntry(int someKey) const
{
// returns a weak_ptr from mThings, or null if a Thing
// that has someKey was not found
return std::weak_ptr<Thing>();
// or
// return {};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5934 次 |
| 最近记录: |