我想用unique_ptr没有成功的数组。
声明unique_ptr大小的正确方法是什么?
(大小是一些参数)。C++ unique_ptr和数组
unique_ptr<A[]> ptr = make_unique<A[]>(size);
下面是一个例子:
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}
这不是工作,但是,如果我删A的构造函数,它的工作原理。
我想3代表数组的大小,而不是A的构造函数的参数,我该如何做到这一点?
提示:使用4个空格缩进标记文本代码 – Drop
为什么不使用'std :: unique_ptr> ptr = make_unique >(3);'? –
或者'std :: unique_ptr> ptr = make_unique >();' –