使用 {a,b,c} 作为参数的构造函数或 {a,b,c} 实际上在做什么?
Constructor using {a,b,c} as argument or what is {a,b,c} actually doing?
我知道,我可以像这样初始化数据。
int array[3] = { 1, 2, 3 };
甚至
int array[2][2] = { {1, 2}, {3, 4} };
我也可以用std::vector
std::vector<int> A = { 1, 2, 3 };
假设我想自己编写 class:
class my_class
{
std::vector< int > A;
public:
//pseudo code
my_class(*x) { store x in A;} //with x={ {1, 2}, {3, 4} }
// do something
};
是否可以编写这样的构造函数,如何实现?这是什么说法
{{1, 2}, {3, 4}}
实际上在做什么?
我总是发现,你可以用这种方式初始化数据,但从来不知道它到底在做什么。
调用list initialization and you need a std::initilizer_list构造函数,在你的my_class
中实现。
参见(Live Demo)
#include <iostream>
#include <vector>
#include <initializer_list> // std::initializer_list
class my_class
{
std::vector<int> A;
public:
// std::initilizer_list constructor
my_class(const std::initializer_list<int> v) : A(v) {}
friend std::ostream& operator<<(std::ostream& out, const my_class& obj) /* noexcept */;
};
std::ostream& operator<<(std::ostream& out, const my_class& obj) /* noexcept */
{
for(const int it: obj.A) out << it << " ";
return out;
}
int main()
{
my_class obj = {1,2,3,4}; // now possible
std::cout << obj << std::endl;
return 0;
}
您可以在构造函数中使用 initializer_list 来获得这样的选项。
struct X {
X() = default;
X(const X&) = default;
};
struct Q {
Q() = default;
Q(Q const&) = default;
Q(std::initializer_list<Q>) {}
};
int main() {
X x;
X x2 = X { x }; // copy-constructor (not aggregate initialization)
Q q;
Q q2 = Q { q }; // initializer-list constructor (not copy constructor)
}
how is it possible?
如果你有初始化列表。
#include <initializer_list> // use std::initializer_list template
my_class(const std::initializer_list<int>& v) : A(v) {}
What is this statement "{{1, 2}, {3, 4}}" actually doing?
If the initializer is a (non-parenthesized) braced-init-list or is =
braced-init-list, the object or reference is list-initialized.
我知道,我可以像这样初始化数据。
int array[3] = { 1, 2, 3 };
甚至
int array[2][2] = { {1, 2}, {3, 4} };
我也可以用std::vector
std::vector<int> A = { 1, 2, 3 };
假设我想自己编写 class:
class my_class
{
std::vector< int > A;
public:
//pseudo code
my_class(*x) { store x in A;} //with x={ {1, 2}, {3, 4} }
// do something
};
是否可以编写这样的构造函数,如何实现?这是什么说法
{{1, 2}, {3, 4}}
实际上在做什么?
我总是发现,你可以用这种方式初始化数据,但从来不知道它到底在做什么。
调用list initialization and you need a std::initilizer_list构造函数,在你的my_class
中实现。
参见(Live Demo)
#include <iostream>
#include <vector>
#include <initializer_list> // std::initializer_list
class my_class
{
std::vector<int> A;
public:
// std::initilizer_list constructor
my_class(const std::initializer_list<int> v) : A(v) {}
friend std::ostream& operator<<(std::ostream& out, const my_class& obj) /* noexcept */;
};
std::ostream& operator<<(std::ostream& out, const my_class& obj) /* noexcept */
{
for(const int it: obj.A) out << it << " ";
return out;
}
int main()
{
my_class obj = {1,2,3,4}; // now possible
std::cout << obj << std::endl;
return 0;
}
您可以在构造函数中使用 initializer_list 来获得这样的选项。
struct X {
X() = default;
X(const X&) = default;
};
struct Q {
Q() = default;
Q(Q const&) = default;
Q(std::initializer_list<Q>) {}
};
int main() {
X x;
X x2 = X { x }; // copy-constructor (not aggregate initialization)
Q q;
Q q2 = Q { q }; // initializer-list constructor (not copy constructor)
}
how is it possible?
如果你有初始化列表。
#include <initializer_list> // use std::initializer_list template
my_class(const std::initializer_list<int>& v) : A(v) {}
What is this statement "{{1, 2}, {3, 4}}" actually doing?
If the initializer is a (non-parenthesized) braced-init-list or is
=
braced-init-list, the object or reference is list-initialized.