标签: shared-ptr

何时使用虚拟析构函数?

我对大多数OO理论有了深刻的理解,但让我困惑的一件事是虚拟析构函数.

我认为无论什么以及链中的每个对象,析构函数总是会被调用.

你什么时候打算让它们成为虚拟的?为什么?

c++ polymorphism shared-ptr virtual-destructor

1420
推荐指数
13
解决办法
66万
查看次数

我们应该通过引用还是通过值传递shared_ptr?

当一个函数采用shared_ptr(来自boost或C++ 11 STL)时,你传递它:

  • 通过const引用: void foo(const shared_ptr<T>& p)

  • 或按价值:void foo(shared_ptr<T> p)

我更喜欢第一种方法,因为我怀疑它会更快.但这真的值得吗还是还有其他问题吗?

您能否说出您选择的原因或案例,为什么您认为无关紧要.

c++ boost shared-ptr c++11

253
推荐指数
8
解决办法
10万
查看次数

C++中make_shared和普通shared_ptr的区别

std::shared_ptr<Object> p1 = std::make_shared<Object>("foo");
std::shared_ptr<Object> p2(new Object("foo"));
Run Code Online (Sandbox Code Playgroud)

许多google和stackoverflow帖子就在这里,但我无法理解为什么make_shared比直接使用更有效shared_ptr.

有人可以一步一步解释我创建的对象序列和两者所做的操作,这样我就能理解make_shared效率如何.我在上面给出了一个例子供参考.

c++ shared-ptr c++11

252
推荐指数
5
解决办法
9万
查看次数

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

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

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

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

223
推荐指数
4
解决办法
17万
查看次数

如何在只有受保护或私有构造函数的类上调用:: std :: make_shared?

我有这个代码不起作用,但我认为意图很明确:

testmakeshared.cpp

#include <memory>

class A {
 public:
   static ::std::shared_ptr<A> create() {
      return ::std::make_shared<A>();
   }

 protected:
   A() {}
   A(const A &) = delete;
   const A &operator =(const A &) = delete;
};

::std::shared_ptr<A> foo()
{
   return A::create();
}
Run Code Online (Sandbox Code Playgroud)

但是我在编译时遇到了这个错误:

g++ -std=c++0x -march=native -mtune=native -O3 -Wall testmakeshared.cpp
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:52:0,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/memory:86,
                 from testmakeshared.cpp:1:
testmakeshared.cpp: In constructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc) [with _Tp = A, _Alloc = std::allocator<A>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:518:8:   instantiated from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr c++11

162
推荐指数
10
解决办法
5万
查看次数

shared_ptr到一个数组:它应该被使用?

只是一个小问题shared_ptr.

使用shared_ptr指向数组是一个好习惯吗?例如,

shared_ptr<int> sp(new int[10]);
Run Code Online (Sandbox Code Playgroud)

如果没有,那么为什么不呢?我已经意识到的一个原因是不能增加/减少shared_ptr.因此,它不能像正常指向数组的指针一样使用.

c++ shared-ptr c++11

157
推荐指数
2
解决办法
8万
查看次数

为类成员使用智能指针

我无法理解智能指针在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万
查看次数

为什么我要std :: move一个std :: shared_ptr?

我一直在查看Clang源代码,我找到了这个片段:

void CompilerInstance::setInvocation(
    std::shared_ptr<CompilerInvocation> Value) {
  Invocation = std::move(Value);
}
Run Code Online (Sandbox Code Playgroud)

我为什么要std::move一个std::shared_ptr

在共享资源上转移所有权是否有任何意义?

为什么我不这样做呢?

void CompilerInstance::setInvocation(
    std::shared_ptr<CompilerInvocation> Value) {
  Invocation = Value;
}
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers shared-ptr move-semantics c++11

133
推荐指数
6
解决办法
3万
查看次数

为什么std :: shared_ptr <void>有效

我发现一些代码使用std :: shared_ptr在shutdown时执行任意清理.起初我认为这段代码不可行,但后来我尝试了以下内容:

#include <memory>
#include <iostream>
#include <vector>

class test {
public:
  test() {
    std::cout << "Test created" << std::endl;
  }
  ~test() {
    std::cout << "Test destroyed" << std::endl;
  }
};

int main() {
  std::cout << "At begin of main.\ncreating std::vector<std::shared_ptr<void>>" 
            << std::endl;
  std::vector<std::shared_ptr<void>> v;
  {
    std::cout << "Creating test" << std::endl;
    v.push_back( std::shared_ptr<test>( new test() ) );
    std::cout << "Leaving scope" << std::endl;
  }
  std::cout << "Leaving main" << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序给出了输出:

At begin of main. …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr c++11

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