变量的定义,强制类型转换

类型 变量名 { }; 或者 类型 变量名 = { };

使用List Initialization (列表初始化)的好处,高精度变低精度,会提示错误。

    int x1{ 1.123 };//提示错误
    //int x1 = 1.123;//不提示错误,x1=1

常见类型写法如下:

    int x{};//int x=0;
    int y{ 10 };
    char c{ 'a' };
    int arr[]{1, 2, 3};//int arr[]={1,2,3};
    char s[]{"hello"};

强制类型转换:static_cast<type> (value)

    cout << static_cast<int> (2.5);
    cout << static_cast<double>(2);
原文地址:https://www.cnblogs.com/xixixing/p/12030601.html