在向量初始化中调用对象的非默认构造函数
我想在构造函数的初始化列表中初始化一个向量。该向量由具有参数化构造函数的对象组成。我所拥有的是:
Class::Class() :
raster_(std::vector< std::vector<Cell> > (60, std::vector<Cell>(80)))
{
...
如何在上一行中使用两个参数调用 Cell::Cell ?显而易见:
raster_(std::vector< std::vector<Cell(true,true)> > (60, std::vector<Cell(true,true)>(80)))
没有用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该尝试:
请注意,我删除了无用的
std::vector >
来自初始化器。另请注意,根据复制Cell
的成本,这可能非常无效:std::vector |
< code>Cell(true, true)std::vector >
通过复制提供的向量(本身包含 80 个元素)60 倍!You should try :
Note that I removed the useless
std::vector<std::vector<Cell> >
from the initializer. Also be aware that this could be highly ineffective depending on the cost of copying aCell
:std::vector<Cell>
by copying 80 times the provided valueCell(true, true)
std::vector<std::vector<Cell> >
by copying 60 times the provided vector (which contains 80 elements itself) !:raster_(std::vector< std::vector > (60, std::vector(80, Cell(true, true)))); | |
如果 raster_ 是接受向量的东西。如果 raster_ 本身是向量,则像这样
:raster(60, std::vector(80, Cell(true, true))) |
:raster_(std::vector< std::vector<Cell> > (60, std::vector<Cell>(80, Cell(true, true))));
if raster_ is something that takes the vector. If raster_ itself is the vector then like this
:raster(60, std::vector<Cell>(80, Cell(true, true)))