如何创建一个构造函数,让我可以使用花括号初始化列表进行构造?
我有类 Phenotype 具有以下构造函数:
Phenotype(uint8 init[NUM_ITEMS]);
我可以创建这样的表型:
uint8 data[] = {0,0,0,0,0};
Phenotype p(data);
但是当我尝试创建这样的表型时出现错误:
Phenotype p = {0,0,0,0,0};
输出:
$ make
g++ -Wall -g main.cpp -std=c++0x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:109: error: no matching function for call to ‘Phenotype::Phenotype(<brace-enclosed initializer list>)’
main.cpp:37: note: candidates are: Phenotype::Phenotype(uint8*)
该错误似乎表明有一种方法可以定义一个构造函数,该构造函数需要一个大括号括起来的初始值设定项列表。有谁知道如何做到这一点?
I have class Phenotype with the following constructor:
Phenotype(uint8 init[NUM_ITEMS]);
I can create a Phenotype like this:
uint8 data[] = {0,0,0,0,0};
Phenotype p(data);
But I get an error when I try to create one like this:
Phenotype p = {0,0,0,0,0};
Output:
$ make
g++ -Wall -g main.cpp -std=c++0x
main.cpp: In function ‘int main(int, char**)’:
main.cpp:109: error: no matching function for call to ‘Phenotype::Phenotype(<brace-enclosed initializer list>)’
main.cpp:37: note: candidates are: Phenotype::Phenotype(uint8*)
The error seems to indicate that there is a way to define a constructor which takes a brace-enclosed initializer list. Does anyone know how this might be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它只能用于聚合(数组和某些类。与流行的看法相反,这也适用于许多非 Pod)。编写接受它们的构造函数是不可能的。
既然您将其标记为“C++0x”,那么这是可能的。神奇的词是“初始化列表构造函数”。然而
,这样的初始化将默认构造数组,然后使用赋值运算符。如果您的目标是速度和安全(由于太多的初始化程序,您会遇到编译时错误!),您还可以使用带有可变参数模板的普通构造函数。
不过,这可能比需要的更通用(通常一个初始化列表就完全足够了,特别是对于普通整数)。它受益于完美转发,因此可以将右值参数移动构造到数组元素中
这是一个艰难的选择!
编辑更正,最后一个也可以,因为我们没有显式构造函数,所以它可以使用
Phenotype
的复制构造函数,构造一个临时Phenotype
对象并将其复制到p3
。但这并不是我们真正希望的调用方式:)It can only be done for aggregates (arrays and certain classes. Contrary to popular belief, this works for many nonpods too). Writing a constructor that takes them is not possible.
Since you tagged it as "C++0x", then this is possible though. The magic words is "initializer-list constructor". This goes like
However, such initialization will default construct the array and then use the assignment operator. If you aim for speed and safety (you get compile time errors for too many initializers!), you can also use an ordinary constructor with a variadic template.
This can be more generic than needed though (often an initializer_list completely suffices, especially for plain integers). It benefits from perfect forwarding, so that an rvalue argument can be move constructed into an array element
It's a hard choice!
Edit Correction, the last one works too, as we didn't make the constructor
explicit
, so it can use the copy constructor ofPhenotype
, constructing a temporaryPhenotype
object and copy it over top3
. But that's not what we really would want the calls to be :)在 C++0x 中,您似乎可以为此创建一个构造函数。我自己没有经验,但看起来它的名字是 初始化列表构造函数。
In C++0x it seems you can create a constructor for this. I have no experience with it myself, but it looks like it's called initializer list-constructor.
您需要使用 std::initializer_list 模板类型。例子:
You need to use the std::initializer_list template type. Example: