如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
How can I use unique pointers for an array of parameterized objects in C++ using Visual Studio 2017?
考虑一个类,其默认构造函数将文件路径作为参数。
class Test
{
public:
Test(const std::string& filepath);
...
...
};
现在,我希望在VS2017中使用唯一指针创建并初始化一个Test
对象数组。
int main()
{
std::unique_ptr<Test[]> m_Tests;
int testCount = 2;
std::string path1, path2;
m_Tests = std::make_unique<Test[]>(testCount); // This line gives a compilation error
m_Tests[0] = std::make_unique<Test>(path1);
m_Tests[1] = std::make_unique<Test>(path2);
}
我怎样才能做到这一点?
g++9.2.0告诉我您缺少默认的构造函数,即没有参数的构造函数。添加这样的构造函数效果很好。如果这不是您想要的,您可以创建unique_ptr的数组,因此std::unique_ptr<std::unique_ptr<Test>[]>
和之后手动初始化每个元素,类似于以下内容:
#include <memory>
#include <algorithm>
#include <iostream>
struct Test {
std::string str_;
Test(std::string const& str) : str_(str) { }
void print() { std::cout << str_ << 'n'; }
};
int main()
{
std::unique_ptr<std::unique_ptr<Test>[]> m_Tests;
int testCount = 2;
std::string path1{"a"}, path2{"b"};
m_Tests = std::make_unique<std::unique_ptr<Test>[]>(testCount);
std::array<std::string, 2> paths{path1, path2};
std::transform(paths.begin(), paths.end(), &m_Tests[0],
[](auto const& p) { return std::make_unique<Test>(p); });
for (int i = 0 ; i < testCount ; ++i) {
m_Tests[i]->print();
}
}
std::make_unique
没有过载,因此您需要直接使用new
:
m_Tests.reset(new Test[testCount]{path1, path2});
但是,只有当testCount
是常量表达式时,这才会编译,因此需要将定义int testCount = 2;
更改为const int
或constexpr int
。
如果testCount
不是常量表达式,则需要为运行时testCount
小于2
的情况定义默认构造函数。
所以,实际上,你可能想忽略testCount
,只需要推导数组大小:
m_Tests.reset(new Test[]{path1, path2});
如果你只使用std::vector
:会容易得多
std::vector<Test> m_Tests;
//...
m_Tests.emplace_back(path1);
m_Tests.emplace_back(path2);
不如使用std::array,然后去掉testCount(或将其用作constexp(,代码如下所示。
class Test
{
public:
Test(const std::string& filepath){}
};
int main()
{
constexpr int testCount = 2;
std::unique_ptr<std::array<Test, testCount>> m_Tests;
std::string path1, path2;
m_Tests = std::make_unique<std::array<Test, testCount>>(std::array<Test, testCount>{path1,path2});
}
相关文章:
- 销毁C++中动态分配的内存(数组对象)
- 数组对象的生存期是否在重用其元素存储时结束?
- 为什么顶点数组对象会导致错误?
- 具有纯虚函数和指针数组对象类型的父类的指针数组
- 这是使用构造函数初始化数组对象的最佳方法吗?
- OpenGL 顶点数组对象与 tinyobjloader
- 将数组/对象/结构列表从C#库中传递给C MFC应用程序
- C++ RapidJson 帮助反序列化数组对象
- ptrdiff_t可以表示指向同一数组对象元素的指针的所有减法吗?
- 检查成员函数是否返回临时对象或数组对象
- 为什么 std::variant 不能容纳数组对象类型,而联合可以?
- 当数组对象以函数参数传递时,为什么复制构造函数会自称
- 如何使用箭头指针打印出一类数组对象,这些对象中有多个分数
- C++17 std::shared_ptr<> 类数组对象的重载运算符 []
- 添加两个具有运算符重载的数组对象,从而导致分段错误
- opengl:两个不同的矢量可以绑定到同一个顶点数组对象吗
- 使用相同的数据填充数组对象或使用指针
- 方法用于最快的分配,并且不需要将动态大小的数组对象作为局部变量
- 如何将2d数组对象传递给c++中的函数
- ReferenceTable溢出(jni-android),数组对象释放