Pan*_*nch 4 c++ visual-c++ c++14
我指的是问题,我们可以创建一个std::unique_ptr已删除默认构造函数的类的数组,如下所示,如何传递string参数.
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class A
{
string str;
public:
A() = delete;
A(string _str): str(_str) {}
string getStr()
{
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
unique_ptr<A[]> arr[3] = make_unique<A[]>(3);
// Do something here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于一组智能指针:
unique_ptr<A> ptr[3];
for (auto& p : ptr)
p = make_unique<A>("hello");
Run Code Online (Sandbox Code Playgroud)