Consider the class Foo and Bar:
class Foo
{
public:
Foo() = default;
Foo(int);
};
class Bar
{
public:
Bar() = default;
Bar(const std::vector<int> & v);
private:
std::vector<Foo> data_;
};
How to write the constructor Bar(const std::vector<int> & v) so that each element of v can initialize a Foo object with Foo(int)?
You can use:
Bar(const std::vector<int> & v) : data_(v.begin(), v.end()) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With