初期化(new)

1016 ワード


#include<iostream>
using namespace std;

int init0;
int main()
{
    // 2   c++11       
    int a = {3};//   !!!
    int b{2};//extended initializer lists only available with -std=c++11 or -std=gnu++11 [    ]
    cout << a << endl;
    cout << b << endl;

    long double c = 123.123456789;
    //           !        error 
    int d{c},e={c};//narrowing conversion of ‘c’ from ‘long double’ to ‘int’ inside { } is ill-formed in C++11 [-Wnarrowing]
    cout << d << endl;
    cout << e << endl;

    int init1;
    cout << "init0:" << init0 << endl;
    cout << "init1:" << init1 << endl;
    return 0;
}


3
2
123
123
init0:0
init1:134519912

変数を初期化しないのが万悪の源だと思っていたのに、なぜc++は関数内の変数にdefault valueを与えないのだろうか.