initializer_list可以让我们像数组初始化列表一样初始化我们的类。
编译器遇到一个初始化列表时,首先会将其转换成为一个std::initializer_list. 所以在创建我们的类的时候,我们创建一个构造函数,该构造函数是以std::initializer_list作为参数,那么我们就可以使用初始化列表作为输入的参数。
std::initializer_list
header:
#include <cassert> // for assert()
#include <initializer_list> // for std::initializer_list
#include <iostream>
class IntArray
{
private:
int m_length{};
int *m_data{};
public:
IntArray() = default;
IntArray(int length) :
m_length{ length },
m_data{ new int[length]{} }
{
}
IntArray(std::initializer_list<int> list) : // allow IntArray to be initialized via list initialization
IntArray(static_cast<int>(list.size())) // use delegating constructor to set up initial array
{
// Now initialize our array from the list
int count{ 0 };
for (auto element : list)
{
m_data[count] = element;
++count;
}
}
~IntArray()
{
delete[] m_data;
// we don't need to set m_data to null or m_length to 0 here, since the object will be destroyed immediately after this function anyway
}
IntArray(const IntArray&) = delete; // to avoid shallow copies
IntArray& operator=(const IntArray& list) = delete; // to avoid shallow copies
int& operator[](int index)
{
assert(index >= 0 && index < m_length);
return m_data[index];
}
int getLength() const { return m_length; }
};
int main()
{
IntArray array{ 5, 4, 3, 2, 1 }; // initializer list
for (int count{ 0 }; count < array.getLength(); ++count)
std::cout << array[count] << ' ';
return 0;
}