stdinitializerlist

Constructor using {a,b,c} as argument or what is {a,b,c} actually doing?

六月ゝ 毕业季﹏ 提交于 2019-12-12 08:49:03
问题 I know, that I can initialize data like this. int array[3] = { 1, 2, 3 }; or even int array[2][2] = { {1, 2}, {3, 4} }; I can also do it with std::vector std::vector<int> A = { 1, 2, 3 }; Let's say I want to write my own 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 }; Is it possible to write such a constructor and how is it possible? What is this statement {{1, 2}, {3, 4}} actually doing? I always

Constructor using {a,b,c} as argument or what is {a,b,c} actually doing?

吃可爱长大的小学妹 提交于 2019-12-04 06:32:59
I know, that I can initialize data like this. int array[3] = { 1, 2, 3 }; or even int array[2][2] = { {1, 2}, {3, 4} }; I can also do it with std::vector std::vector<int> A = { 1, 2, 3 }; Let's say I want to write my own 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 }; Is it possible to write such a constructor and how is it possible? What is this statement {{1, 2}, {3, 4}} actually doing? I always just find, that you can initialize data in this way, but never what it is doing precisely. It is