标签: smart-pointers

为什么我无法在赋值中将 unique_ptr 转换为原始指针?

在编写简单的二叉搜索树插入时,在g++ 4.7中遇到编译错误

\n\n
error: cannot convert \xe2\x80\x98node_ptr {aka std::unique_ptr<node>}\xe2\x80\x99 to \xe2\x80\x98node*\xe2\x80\x99 in assignment\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于函数node* n = root.get()中的行bst_insert。我不明白为什么?

\n\n
struct node;\n\ntypedef std::unique_ptr<node> node_ptr;\n\nstruct node {\n        node(int k) : key(k) {};\n        int key;\n        node_ptr left = nullptr;\n        node_ptr right = nullptr;\n};\n\nvoid bst_insert(node_ptr& root, node_ptr z) {\n    node* p = nullptr;\n    node* n = root.get();\n    while (n != nullptr) {\n        p = n;\n        n = z->key < n->key ? n->left : n->right;\n    }\n    if (p == nullptr)\n        root …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers c++11

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

std::shared 从基类到派生类的 std::dynamic_pointer_cast 返回 NULL

我有一个函数,它接受类型的共享指针Base,然后std::dynamic_pointer_cast指向派生类型。但是,派生指针是 NULL,我不明白为什么。我已确保在我的基类中包含一个虚拟析构函数。我不想使用静态强制转换,因为这不能保证我的派生成员变量和函数被保留?

代码如下:

基类:

class Base
{
public:
    mType get_type()
    {
        return msg_type;
    }

    void set_type(mType type)
    {
        msg_type = type;
    }

    virtual ~cMsg() = default;
protected:
    mType msg_type;
    message msg;
};
Run Code Online (Sandbox Code Playgroud)

派生类:

class Derived : public Base
{
public:
    void set_i(int j)
    {
        i = j;
    }

    int get_i()
    {
        return i;
    }
private:
    int i;
};
Run Code Online (Sandbox Code Playgroud)

功能执行演员表:

void callback(std::shared_ptr<Base> msg_received)
{
    std::cout<< "Callback called\n";
    auto real_msg = std::dynamic_pointer_cast<Derived>(msg_received);

    if (real_msg != NULL)
    {
        std::cout …
Run Code Online (Sandbox Code Playgroud)

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

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

unique_ptr 的多个实例

在发现我需要我创建的delete任何new指针后,我很快意识到我的项目充满了内存泄漏,而我什至不知道。所以提示我使用智能指针。但是,在尝试创建智能指针的多个实例时,我遇到了问题。我创建了一个 SSCE 来更好地解释这一点。

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <memory>
#include "classa.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    std::unique_ptr<ClassA> classa; //<----- a smart pointer of a Class type
};

#endif // MAINWINDOW_H
Run Code Online (Sandbox Code Playgroud)

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "classa.h"
#include <QDebug>

QVector<ClassA*> classes;//<------ QVector that contains instances of ClassA
                                    //So I can retrieve …
Run Code Online (Sandbox Code Playgroud)

c++ qt class smart-pointers

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

与“operator[]”不匹配(操作数类型为“std::unique_ptr&lt;std::vector&lt;int&gt; &gt;”和“int”)

我有一个std::unique_ptr<std::vector<int>>,我正在尝试使用运算符访问一个元素[]。如何访问 中包含的向量的特定索引std::unique_ptr

#include <memory>
#include <vector>

int main()
{
    std::unique_ptr<std::vector<int>> x;
    x[0] = 1;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

c++ smart-pointers vector std

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

从堆栈分配的原始指针构造智能指针

有人可以告诉我这里发生了什么吗?

int* stackint = new int(5);

{
    std::unique_ptr<int> myInt(stackint);    
    *myInt = 8;

}

std::cout << *stackint; // 0
Run Code Online (Sandbox Code Playgroud)

这里到底发生了什么?我理解智能指针,当你用 new 或 make_unique 构造它们时,当你将堆栈指针传递给它们的构造函数时会发生什么?

c++ smart-pointers

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

如何从C++中的弱指针访问类成员函数?

我对智能指针完全是新手,而且从未接触过weak_ptrC++。

我在 class 中有一个函数Y,它接受weak_ptrclass 的参数 a X

在类的函数内部Y,我需要X通过weak_ptr.

作为参考,这里是一个粗略的类定义:

Y.cpp

class Y {
  public : Y();
           ~Y();
           int foo(std::weak_ptr<X> x);
};
Run Code Online (Sandbox Code Playgroud)

X.cpp

class X {
 public : std::string func1();
          int func2();
};
Run Code Online (Sandbox Code Playgroud)

foo()在类的函数内部Y,我需要能够访问函数func1func2使用weak_ptr x.

我不知道为什么我需要使用weak_ptr,但这就是我应该实现的。

我无法找到任何使用weak_ptr.

c++ smart-pointers weak-ptr

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

关于从函数返回 unique_ptr 的问题

根据文件,其中说

我们已经在 return 语句中对本地值和函数参数进行了隐式移动。下面的代码编译得很好:

std::unique_ptr<T> f(std::unique_ptr<T> ptr) {
    return ptr;
} 
Run Code Online (Sandbox Code Playgroud)

但是,下面的代码无法编译

std::unique_ptr<T> f(std::unique_ptr<T> && ptr) {
    return ptr;
} 
Run Code Online (Sandbox Code Playgroud)

相反,您必须输入

std::unique_ptr<T> f(std::unique_ptr<T> && ptr) {
    return std::move(ptr);
}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

1.确实没有复制构造函数std::unique_ptr<T>,不应该有一个函数可以接受声明为 的参数std::unique_ptr<T>。请参阅此代码片段,它无法编译。

#include <memory>

class FooImage{};

std::unique_ptr<FooImage> FoolFunc(std::unique_ptr<FooImage> foo)
{
    return foo;
}

int main()
{

    std::unique_ptr<FooImage> uniq_ptr(new FooImage);

    FoolFunc(uniq_ptr);
}
Run Code Online (Sandbox Code Playgroud)

2.为什么

std::unique_ptr<T> f(std::unique_ptr<T> && ptr) {
    return ptr;
} 
Run Code Online (Sandbox Code Playgroud)

不编译?

有人可以解释一下这个问题吗?

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

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

即使应用了重置,智能指针仍然引用原始指针

请找到我从https://www.geeksforgeeks.org/auto_ptr-unique_ptr-shared_ptr-weak_ptr-2/获取的用于测试智能指针的代码。

// C++ program to demonstrate shared_ptr
#include <iostream>
#include <memory>

class A {
public:
    void show()
    {
        std::cout << "A::show()" << std::endl;
    }
};

int main()
{
    std::shared_ptr<A> p1(new A);
    std::cout << p1.get() << std::endl;
    p1->show();
    std::shared_ptr<A> p2(p1);
    p2->show();
    std::cout << p1.get() << std::endl;
    std::cout << p2.get() << std::endl;

    // Returns the number of shared_ptr objects
    // referring to the same managed object.
    std::cout << p1.use_count() << std::endl;
    std::cout << p2.use_count() << std::endl;

    // Relinquishes ownership of p1 …
Run Code Online (Sandbox Code Playgroud)

c++ g++ smart-pointers c++11

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

指向属于另一个共享指针的内存部分的共享指针

我知道共享指针意味着共享相同的内存。但是,如果我的共享指针指向的元素不是另一个共享指针内存中的第一个元素怎么办?

考虑一个原始指针示例:

int* array = new int[10];
int* segment = &array[5];
Run Code Online (Sandbox Code Playgroud)

array我可以用共享segment指针做同样的事情吗?在这种情况下他们会计算参考文献吗?

c++ smart-pointers

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

如何使用智能指针来存储

我有两节课。一个类(Person)有一个由另一类(Student)的指针组成的向量集合。在运行时,Person 类将调用一个方法,该方法将在向量中存储指向 Student 类的指针。我一直在尝试使用智能指针来做到这一点,以避免可能出现的内存泄漏问题,但我正在努力这样做。我该怎么办呢?

我的目标是让 Person 类拥有代码中其他地方存在的对象的句柄

Class Student
{
  public:
    string studentName
    Student(string name){
      studentName = name;
    }
}

Class Person
{
  public:
    vector <Student*> collection;
    getStudent()
   {
    cout << "input student name";
    collection.push_back(new Student(name));
   }
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers class smart-pointers

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

标签 统计

c++ ×10

smart-pointers ×10

c++11 ×4

class ×2

casting ×1

g++ ×1

pointers ×1

qt ×1

shared-ptr ×1

std ×1

unique-ptr ×1

vector ×1

weak-ptr ×1