I want to create a constructor, that can get an unknown number of ints so if someone wants to create a new MyObj(1,2)
it should work, aswell as new MyObj(1,2,3,2,5)
or new MyObj(1,2,3,2,..,n)
.
Thank you
I want to create a constructor, that can get an unknown number of ints so if someone wants to create a new MyObj(1,2)
it should work, aswell as new MyObj(1,2,3,2,5)
or new MyObj(1,2,3,2,..,n)
.
Thank you
There's a number of ways to achieve that (including the good old var_args
as proposed as duplicate). One of the simplest is using a std::initializer_list
:
class MyClass {
std::vector<int> v;
public:
MyClass(std::initializer_list init) : v(init) {}
};
Yeah, you can use std::initializer_list
so long as the types are all the same.