我想用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 >();' –