PS:以下代码在vs2015编译通过~~~
#include <iostream>
#include <vector>using namespace std;
//With C++11, everything can be initialized in much the same way.
//Examples:
//1.Initialization of dynamically allocated arrays:
//int *pi = new int[5]{1, 2, 3, 4, 5};
//2.Initialization of an array member variable:
//class Array
//{
// int array[3];
//public:
// Array(int x, int y, int z) :array{x, y, z}{}
//};
//3.Initialization of a STL container:
//vector<int> v{ 1, 2, 3, 4 };
//4.Implicitly initialize objects to return:
// return {bar, var};
//5.Implicitly initialize a function parameter:
//foo({ bar, var });
/*One thing, there are differences in terms of priorities, it's set in this order:
1.initializer_list
2.regular constructor 常规构造
3.aggregate initialization 聚合初始化*/
class A
{
public:
int mx;
double my;
};
class B
{
public:
B(int x, double y) :mx(x), my(y){}
int mx;
double my;
};
class C
{
public:
C(int x, double y) :mx(x), my(y){}
C(const initializer_list<int>& v)
{
mx = *(v.begin());
my = *(v.begin() + 1);
}
int mx;
double my;
};
int main()
{
// Aggregate initialization
A a{ 1, 1.5 };
// Regular constructor
B b{ 2, 2.5 };
//Initializer_list
C c{ 3, 4 };
cout << a.mx << " , " << a.my << endl;
cout << b.mx << " , " << b.my << endl;
cout << c.mx << " , " << c.my << endl;
system("pause");
return 0;
}