标签: smart-pointers

1730
推荐指数
11
解决办法
54万
查看次数

什么时候std :: weak_ptr有用吗?

我开始研究C++ 11的智能指针,我没有看到任何有用的用法std::weak_ptr.有人能告诉我什么时候std::weak_ptr有用/必要吗?

c++ smart-pointers shared-ptr weak-ptr c++11

247
推荐指数
11
解决办法
11万
查看次数

我什么时候使用哪种指针?

好吧,所以我最后一次以C++为生,std::auto_ptr所有的std lib都可用,而且boost::shared_ptr风靡一时.我从未真正研究过提供的其他智能指针类型.我知道C++ 11现在提供了一些类型的提升,但不是全部.

那么有人有一个简单的算法来确定何时使用哪个智能指针?优选地包括关于哑指针(诸如原始指针T*)和其他增强智能指针的建议.(像这样的东西会很棒).

c++ pointers smart-pointers c++-faq c++11

224
推荐指数
4
解决办法
3万
查看次数

对于带有数组的unique_ptr有什么用处吗?

std::unique_ptr 支持数组,例如:

std::unique_ptr<int[]> p(new int[10]);
Run Code Online (Sandbox Code Playgroud)

但它需要吗?可能使用std::vector或更方便std::array.

你觉得这个结构有用吗?

c++ smart-pointers c++11

219
推荐指数
12
解决办法
13万
查看次数

智能指针(boost)解释道

以下指针集之间有什么区别?什么时候在生产代码中使用每个指针,如果有的话?

例子将不胜感激!

  1. scoped_ptr

  2. shared_ptr

  3. weak_ptr

  4. intrusive_ptr

你在生产代码中使用boost吗?

c++ boost smart-pointers

218
推荐指数
4
解决办法
8万
查看次数

为什么我不能将unique_ptr push_back到向量中?

这个程序有什么问题?

#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)

c++ stl smart-pointers unique-ptr c++11

192
推荐指数
2
解决办法
13万
查看次数

RAII和C++中的智能指针

在使用C++的实践中,什么是RAII,什么是智能指针,如何在程序中实现这些以及将RAII与智能指针一起使用有什么好处?

c++ smart-pointers raii

189
推荐指数
4
解决办法
6万
查看次数

std :: auto_ptr到std :: unique_ptr

随着新标准的出现(以及某些编译器中已有的部件),新类型std::unique_ptr应该是替代品std::auto_ptr.

它们的用法是否完全重叠(因此我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做))或者我应该注意一些在阅读文档时不明显的差异?

此外,如果它是一个直接替代品(为什么给它一个新的名称),而不仅仅是改善std::auto_ptr.

c++ smart-pointers auto-ptr unique-ptr c++11

182
推荐指数
4
解决办法
6万
查看次数

为什么C++库和框架从不使用智能指针?

我在一些文章中读到,几乎从不使用原始指针.相反,它们应始终包含在智能指针内,无论是作用域还是共享指针.

但是,我注意到像Qt,wxWidgets和像Boost这样的库这样的框架永远不会返回也不会期望智能指针,就好像它们根本就没有使用它们一样.相反,他们返回或期望原始指针.有什么理由吗?当我编写公共API时,我应该远离智能指针,为什么?

只是想知道为什么许多重大项目似乎避免使用智能指针.

c++ api pointers smart-pointers

156
推荐指数
5
解决办法
3万
查看次数

为类成员使用智能指针

我无法理解智能指针在C++ 11中作为类成员的用法.我已经阅读了很多关于智能指针的内容,我想我确实理解了如何unique_ptrshared_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_ptrweak_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++ smart-pointers shared-ptr unique-ptr c++11

146
推荐指数
1
解决办法
8万
查看次数

标签 统计

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