I am new to C++, I need some clarification about the constructor and my question here is:
Please explain how it can be done, or why it can't. I need a deeper understanding about this.
You can use a parameterized constructor to initialize an array. See the following example.
class Foo
{
public:
Foo(int _a, bool _b) : a{_a}, b{_b} {}
private:
int a;
bool b;
};
int main() {
Foo things[] = {{5, true},
{3, false},
{7, true}};
return 0;
}
The array things
is of Foo
objects. I am using uniform initialization to construct 3 Foo
objects in the array. These are relying on the parameterized constructor that I defined in the class.