You can define the so-called initializer constructor constructor and use class iterators std::initializer_listto access the items in the list.
In accordance with the C ++ standard
initializer list constructor, if its first parameter is of type std :: initializer_list or a link, possibly with the qualification cv std :: initializer_list for some type E, and either no other parameters or all other parameters have default arguments
for instance
#include <iostream>
#include <vector>
#include <initializer_list>
class A
{
public:
A() = default;
A( std::initializer_list<int> l ) : v( l ) {}
std::vector<int> v;
};
int main()
{
A a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for ( int x : a.v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
source
share