initialization/init1.cc - Initialization and narrowing. | Lecture 2 - slide 26 : 29 Program 2 |
// Shows initialization with and without narrowing. Initialization with {} prohibits narrowing (causes warnings). #include <iostream> #include <string> void f(double dval, int ival){ int i = dval; // OK - narrowing conversion to int, and initialization char ch = ival; // OK - narrowing conversion to char (skipping higher order bits) and initialization std::cout << "i: " << i << std::endl; // 3 std::cout << "ch: " << ch << std::endl; // L } void g(double dval, int ival){ int i {dval}; // warning: narrowing conversion ... char ch {ival}; // warning: narrowing conversion ... std::cout << "i: " << i << std::endl; // 3 std::cout << "ch: " << ch << std::endl; // L } int main(){ f(3.1415, 1100); g(3.1415, 1100); }