c++ - What's the output of the following code? -
this question has answer here:
- why call default constructor? 3 answers
this code published in http://accu.org/index.php/cvujournal, issue july 2013. couldn't comprehend output, explanation helphful
#include <iostream> int x; struct { i() { x = 0; std::cout << "--c1\n"; } i(int i) { x = i; std::cout << "--c2\n"; } }; class l { public: l(int i) : x(i) {} void load() { i(x); } private: int x; }; int main() { l l(42); l.load(); std::cout << x << std::endl; }
output:
--c1 0
i expecting:
--c2 42
any explanation ?
i(x);
equivalent i x;
, redundant pair of parentheses thrown in. declares variable named x
of type i
, default-initialized; not create temporary instance of i
x
constructor's parameter. see also: most vexing parse
Comments
Post a Comment