This code was published in http://accu.org/index.php/cvujournal, Issue July 2013. I couldn't comprehend the output, any explanation would be helphful
#include <iostream>
int x;
struct i
{
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 was expecting:
--C2
42
Any Explanation ?
i(x);
is equivalent to i x;
, with a redundant pair of parentheses thrown in. It declares a variable named x
of type i
, default-initialized; it does not create a temporary instance of i
with x
as the constructor's parameter. See also: most vexing parse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With