How to define an initializer list constructor in C++

1 Answer

0 votes
#include <iostream>

class Example {
public:
    Example(std::initializer_list<int> args) {
        for (auto val : args)
            std::cout << val << " ";
    }
};

int main() {
    Example arr { 1, 2, 3, 4, 5 }; 
}




/*
run:

1 2 3 4 5 

*/

 



answered Dec 1, 2022 by avibootz

Related questions

2 answers 240 views
1 answer 216 views
1 answer 144 views
1 answer 197 views
1 answer 214 views
...