标签: smart-pointers

获取boost的shared_ptr的指向类型

在我的项目中,我使用 boost::shared_ptr,在一个头文件中,我写道:

typedef boost::shared_ptr<boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> > > queue_ptr;
Run Code Online (Sandbox Code Playgroud)

在另一个源文件中,我使用它:

std::vector<queue_ptr> v;
for (...)
    v.push_back(boost::make_shared(/* #1 */));
Run Code Online (Sandbox Code Playgroud)

在#1中,我想将queue_ptr的点写入类型,而不是

boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> >
Run Code Online (Sandbox Code Playgroud)

有多长啊!

但是boost::shared_ptr中没有typedef,我找到的唯一一个typedef:typedef typename boost::detail::sp_element< T >::type element_type;但是我不知道如何使用它。

有什么帮助吗?坦克很多!

c++ boost smart-pointers shared-ptr

1
推荐指数
1
解决办法
1608
查看次数

嵌套 unique_ptr 和 stl 容器

我最近阅读了有关 RAII 的内容并开始使用它。我试图将图定义为邻接列表并使用unique_ptr. 我知道我可以将它们定义为堆栈对象,但我正在尝试使用unique_ptr's 来习惯它们。

我正在做以下事情

unique_ptr to vector  --
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
                       |-->(unique_ptr)list<edge>
Run Code Online (Sandbox Code Playgroud)

代码:

#include<memory>
#include<vector>
#include<list>
using namespace std;

struct edge {
    int vertex;
    int weight;
};
int vertices = 10;
unique_ptr<vector<unique_ptr<list<struct edge > > > >
    adj_list(new vector<list<struct edge> >(10)); // THIS COMPILES

unique_ptr<vector<unique_ptr<list<struct edge > > > >
    adj_list(new vector<list<struct edge> >(10, new list<edge>())); //THIS GIVES COMPILATION ERROR
Run Code Online (Sandbox Code Playgroud)

谁能帮我纠正这个问题?

编辑:

我对向量是 RAII 类有疑问。如果我执行以下操作

vector<int> *v;
v = new vector<int> (10);
Run Code Online (Sandbox Code Playgroud)

我必须删除变量吗v …

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

1
推荐指数
1
解决办法
1076
查看次数

使用唯一指针调用函数会使我的程序崩溃

我制作了一个非常简单的程序来尝试将唯一指针和继承融合在一起。但是,它最终以退出代码 11 崩溃,我不知道为什么。任何人都可以解释崩溃的原因吗?

//Counter Class, Base class
class Counter {
   public:
    virtual int addStuff(int& x)=0;
  };
Run Code Online (Sandbox Code Playgroud)
//Derived Class, child class of Counter
class Stuff:public Counter {
 public:
  virtual int addStuff(int& x) override;
};
Run Code Online (Sandbox Code Playgroud)
//Main function using unique pointers to call addStuff from Stuff class
int main() {
  int x = 12;
  std::unique_ptr<Stuff> p;
  p->addStuff(x);
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance pointers smart-pointers c++11

1
推荐指数
1
解决办法
109
查看次数

拥有可以指向或引用已在堆栈上分配的不同类型数据的映射的现代方法

使用原始指针,可以这样实现:

#include <iostream>
#include <string>
#include <map>

using namespace std;

class base {
public:
    virtual ~base(){}
};

class derived1 : public base {
public:
    virtual ~derived1(){}
    derived1(const int& other) : val(other) {}
    int val;
};

class derived2 : public base {
public:
    virtual ~derived2(){}
    derived2(const float& other) : val(other) {}
    float val;
};


int main()
{
    derived1 dataA = 4;
    derived2 dataB = 3.0f;

    std::map<std::string, base*> mapOfPointersToData; //Needs to be a pointer because it can be of type deribed1 or …
Run Code Online (Sandbox Code Playgroud)

c++ reference smart-pointers

1
推荐指数
1
解决办法
50
查看次数

std::unique_ptr::get 和 operator&amp; 的不同地址

在下面的例子中使用指针让我感到困惑,所以我可能误解了一些东西。我将尝试展示我的理解方式和一些伪简单的例子。

创建std::unique_ptr名称变量my_int确保它不能被复制,并且它的int对象只有一个所有者。然后我创建一个向量来存储指针并将其放入而不用std::emplace_back(). 现在我想检查 vector 的元素integers[0]在内存中是否与原始元素具有相同的地址my_int。我认为应该是因为无法复制此元素。

示例代码:

auto my_int = std::make_unique<int>(1);
std::vector<std::unique_ptr<int>> integers;
integers.emplace_back(my_int.get());

std::cout<< "Value of my_int: " << *my_int << std::endl;
std::cout << "Address of my_int: " << my_int.get() << std::endl;
std::cout << "Also address of my_int: " << &my_int << std::endl;
std::cout << "Also address of my_int: " << &integers[0]<< std::endl;
std::cout << "Also address of my_int: " << integers[0].get()<< std::endl;
Run Code Online (Sandbox Code Playgroud)

测试结果:

Value of my_int: 1 …
Run Code Online (Sandbox Code Playgroud)

c++ pointers smart-pointers unique-ptr

1
推荐指数
1
解决办法
101
查看次数

Rust 智能指针 std::rc::Rc 和 std::sync::Arc 分别类似于 C++ 智能指针 std::shared_ptr 和 std::atomic_shared_ptr 吗?

锈智能指针之间存在类比std::rc::Rc,并std::sync::Arc与C ++智能指针std::shared_ptrstd::atomic_shared_ptr?对我来说,它们看起来一样,但可能有一些实现上的细微差别。例如在 C++std::shared_ptr中,控制块中的引用计数是原子的,尽管指针本身不是。在 Rust 中是一样的std::rc::Rc吗?

c++ smart-pointers rust

1
推荐指数
1
解决办法
198
查看次数

unique_ptr 两次调用析构函数

我有一个代码块,我正在使用unique_ptr.

class Abc {
public:
    std::string msg;
    Abc(std::string m) {
        msg = m;
        std::cout << "Constructor: " << msg << std::endl;
    }  
    ~Abc() {
        std::cout << "Destructor: " << msg << std::endl;
    }
};


int main() {
    auto p = std::make_unique<Abc>(Abc(__func__));
}
Run Code Online (Sandbox Code Playgroud)

但是析构函数被调用了两次。有没有办法让它只调用一次析构函数?

c++ smart-pointers unique-ptr

1
推荐指数
1
解决办法
94
查看次数

“由于在生成器中使用而发生移动”错误在 Rust 中意味着什么?

我有关于发电机的这个问题:

use tokio::runtime::Runtime;
use tokio::task::JoinHandle;
use std::sync::Arc;

pub fn run(f: Box<dyn Fn() -> Result<(), ()> + Send>) {
    f();
}

fn main() {
    let x = Arc::new(0);
    run(Box::new(move ||{
        let rt = Runtime::new().unwrap();
        let _t = rt.block_on(async move {
            let y = x;
        });
        Ok(())
    }));
}
Run Code Online (Sandbox Code Playgroud)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=de28ecf9e5baf6a017cd6a5230d74d7b

错误:

error[E0507]: cannot move out of `x`, a captured variable in an `Fn` closure
  --> src/main.rs:13:41
   |
10 |       let x = Arc::new(0);
   |           - captured outer variable
...
13 |           let …
Run Code Online (Sandbox Code Playgroud)

smart-pointers move-semantics rust

1
推荐指数
1
解决办法
213
查看次数

为什么 shared_ptr 不删除它的内存?

int main(){

    int* iptr;
    {
    std::shared_ptr<int> sptr = std::make_shared<int>(12);
    iptr = sptr.get();
    }

    std::cout << *iptr;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出

12
Run Code Online (Sandbox Code Playgroud)

我原以为iptr指向的内容会被删除,位于内花括号的末尾。但输出显示 12. 我错过了什么吗?

c++ smart-pointers shared-ptr

1
推荐指数
2
解决办法
102
查看次数

裸指针的替换/重构

我想在类继承情况下替换传统的裸指针用法。

我的意思的例子:

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

using namespace std;

class Base
{
public:
  void doBaseStuff ()
  {
    cout << "base stuff\n";
  }
};

class Derived:public Base
{
public:
  void doDerivedStuff ()
  {
    cout << "derived stuff\n";
  }
};


int main ()
{
 // vector to hold pointers
  vector < Base * >baseCollection;

// generate pointers
  Derived *derivedPtr = new Derived;
  Base* basePtr = new Base;
  
 // use derived pointer for something
  derivedPtr->doDerivedStuff ();
  
 // fill vector with pointers …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers

1
推荐指数
1
解决办法
66
查看次数