我正在尝试使用智能指针执行该程序:
\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}\nRun Code Online (Sandbox Code Playgroud)\n\n我之前用原始指针(*)测试过它并且它有效。尽管如此,当使用智能指针时,我收到以下错误:
\n\nmain.cpp: …Run Code Online (Sandbox Code Playgroud) 我正在阅读 C++ 中的智能指针,我很惊讶超过 99% 的给定示例实际上是相当糟糕的示例,因为在这些情况下可以避免动态分配。我同意仅在 STL 容器不起作用的上下文中使用智能指针。例如,在动态数组 ( std::vector) 中,性能很重要,因此最好拥有经过良好测试的代码而不使用任何智能指针。
这里我认为是一个不好的例子,因为在这种情况下unique_ptr不是解决方案,但堆栈分配将是正确的方法。
MyObject* ptr = new MyObject();
ptr->DoSomething();
delete ptr;
Run Code Online (Sandbox Code Playgroud)
那么什么是 C++ 中智能指针的好例子或用途呢?
换种说法是什么设计模式需要将指针的所有权转移到另一个对象?
由于某种原因,我不能在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) 我有一个类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
这不是创建类的共享指针实例的方法吗?
考虑智能指针的以下用法std::unique_ptr:
std::unique_ptr<char> sp(new(std::nothrow) char[sz]);
Run Code Online (Sandbox Code Playgroud)
我如何检查是否new成功?
我有两个选择:
if(!sp){}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++中的指针?举个例子:
class Object { }
smart_pointer < Object > pInstance;
//wherein I can use the very simple way
Object instance;
Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×1
constructor ×1
dictionary ×1
init ×1
new-operator ×1
nullptr ×1
pointers ×1
shared-ptr ×1
unique-ptr ×1
vector ×1