C++11/C++14 (二)UNIFORM_INITIALIZATION

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值