标签: smart-pointers

C++ 中的智能指针和地图

我正在尝试使用智能指针执行该程序:

\n\n
//File user.h\n#include <list>\n#include <iostream>\n#include <string>\n#include <fstream>\n#include <sstream>\n#include <memory>\n#include <map>\n\nusing namespace std;\n\nclass User {\n    string name;\n    int id;\npublic:\n    User(const string& name, int id) : name(name), id(id) {}\n    int getID() const {return id;}\n    ~User(){}\n};\n\n//File main.c\n#include "user.h"\nusing namespace std;\ntypedef std::shared_ptr<User> UserPtr;\ntypedef map<string, UserPtr> Dict;\n\nint main()\n{\n    Dict dict;\n    dict = new User("Adams", 666);\n    auto it = dict.find("Adams");\n\n    if (it == dict.end())\n        cout << "not found!" << endl;\n\n    else\n        cout << "id3: " << it->second->getID() << endl;\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我之前用原始指针(*)测试过它并且它有效。尽管如此,当使用智能指针时,我收到以下错误:

\n\n
main.cpp: …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary smart-pointers

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

智能指针有真正的用例吗?

我正在阅读 C++ 中的智能指针,我很惊讶超过 99% 的给定示例实际上是相当糟糕的示例,因为在这些情况下可以避免动态分配。我同意仅在 STL 容器不起作用的上下文中使用智能指针。例如,在动态数组 ( std::vector) 中,性能很重要,因此最好拥有经过良好测试的代码而不使用任何智能指针。

这里我认为是一个不好的例子,因为在这种情况下unique_ptr不是解决方案,但堆栈分配将是正确的方法。

MyObject* ptr = new MyObject(); 
ptr->DoSomething(); 
delete ptr; 
Run Code Online (Sandbox Code Playgroud)

那么什么是 C++ 中智能指针的好例子或用途呢?

换种说法是什么设计模式需要将指针的所有权转移到另一个对象?

c++ pointers smart-pointers

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

c ++ std :: vector"this"是"nullptr"

由于某种原因,我不能在datacenter.cpp中使用vector _vec.它说" 这个 "是"nullptr"

请帮助,谢谢<3

datacenter.h

#pragma once
#include <iostream>
#include <vector>

class datacenter
{
public:
    datacenter();
    ~datacenter();

    void get_elements();

    std::vector<float> _vec;
};
Run Code Online (Sandbox Code Playgroud)

datacenter.cpp

#include "datacenter.h"

datacenter::datacenter(){}

void datacenter::get_elements()
{
if (_vec.empty()) { //<---- the error appears here
    std::cout << "empty" << std::endl;
    }
}

datacenter::~datacenter(){}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream>
#include <vector>
#include "datacenter.h"

class datacenter;

int main()
{
std::unique_ptr<datacenter> dc;
dc->get_elements();
}
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers vector init nullptr

-3
推荐指数
1
解决办法
689
查看次数

C ++ shared_ptr不提供调用运算符

我有一个类Foo,其中包含一个shared_ptrclass Foo2作为成员变量。我这样宣告以上观点:

class Foo{
public:
    Foo();

private:
    shared_ptr<Foo2> f2;

};
Run Code Online (Sandbox Code Playgroud)

首先,我可以做以上吗?即不初始化shared_ptr?

Foo我的默认构造函数中,如下所示初始化了shared_ptr变量:

Foo::Foo(){
    //create and calculate parameters a, b and c
    f2(new Foo2(a, b, c));
}
Run Code Online (Sandbox Code Playgroud)

由于Foo2的唯一构造函数具有3个参数。但是,这显示了一个错误:

Type 'shared_ptr<Foo2>' does not provide a call operator

这不是创建类的共享指针实例的方法吗?

c++ constructor smart-pointers shared-ptr

-3
推荐指数
1
解决办法
111
查看次数

如何检查 C++ 智能指针内存分配是否成功?

考虑智能指针的以下用法std::unique_ptr

std::unique_ptr<char> sp(new(std::nothrow) char[sz]);
Run Code Online (Sandbox Code Playgroud)

我如何检查是否new成功?

我有两个选择:

  1. 方法 1 - 检查 bool 值: if(!sp){}
  2. 方法 2 - 与空指针比较: if(sp==nullptr){}

示例(来源

#include <iostream>
#include <memory>
using namespace std;

int main() {
    constexpr long long sz = 1000000e10;
    
    //raw pointer
    auto ptr = new(std::nothrow) char[sz];
    if(ptr==nullptr)
    {
        cout<<"ptr nullptr"<<endl;
    }
    
    //smart pointer
    std::unique_ptr<char> sp(new(std::nothrow) char[sz]);
    
    if(!sp)
    {
        cout<<"sp nullptr bool"<<endl;
    }
    
    if(sp==nullptr)
    {
        cout<<"sp nullptr =="<<endl;
    }
    return 0;
    
}
Run Code Online (Sandbox Code Playgroud)

输出:

std::unique_ptr<char> sp(new(std::nothrow) char[sz]);
Run Code Online (Sandbox Code Playgroud)

显然,方法 1 和方法 …

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

-4
推荐指数
1
解决办法
1424
查看次数

为什么要使用smartpointers?

特别是当智能指针很好用时,为什么还要使用智能指针,如果不鼓励使用C++中的指针?举个例子:

class Object {  }

smart_pointer < Object > pInstance;
//wherein I can use the very simple way
Object instance;
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers

-8
推荐指数
1
解决办法
236
查看次数