#include <iostream>
#include <cassert>
#include <initializer_list>
using namespace std;
class IntArray
{
private:
unsigned m_length = 0;
int *m_data = nullptr;
public:
IntArray(unsigned length)
: m_length(length)
{
m_data = new int[length];
}
IntArray(const std::initializer_list<int> &list)
: IntArray(list.size()) // 메모리 할당
{
int count = 0;
for (auto & element : list)
{
m_data[count] = element;
++count;
}
// for (unsigned count = 0; count < list.size(); ++count)
// m_data[count] = list[count]; // error initializer list는 대괄호를 지원하지 않아서 위와같은 방식으로 초기화해야함
}
~IntArray()
{
delete [] this->m_data;
}
friend ostream & operator << (ostream & out, IntArray & arr)
{
for (unsigned i =0;i<arr.m_length; ++i)
out << arr.m_data[i] << "\n";
out << "\n";
return out;
}
};
int main()
{
int my_arr1[5] = { 1, 2, 3, 4, 5 }; // initializer list
int *my_arr2 = new int[5]{ 1, 2, 3, 4, 5 }; // 동적할당할 때도 사용 가능
auto il = { 10, 20, 30 }; // initializer list를 include 하면 가능
IntArray int_array { 1, 2, 3, 4, 5 };
cout << int_array << "\n";
return 0;
}