this is a little bit difficult to put into words, here is the snippet,
int main() {
int i = 5;
{
int i(i);
i = i+5;
// int i = i;
// i = i + 5;
cout << "inner scope" << endl;
cout << i << endl;
}
cout << "outer scope" << endl;
cout << i << endl;
}
this give the correct output
inner scope
10
outer scope
5
But if I do this,
int main() {
int i = 5;
{
//int i(i);
//i = i+5;
int i = i;
i = i + 5;
cout << "inner scope" << endl;
cout << i << endl;
}
cout << "outer scope" << endl;
cout << i << endl;
}
This throws an runtime exception, and gives wrong output,
inner scope
-858993455
outer scope
5
I feel this seems to be jsomething to do with the difference in the sequence of calling of copy constructor and assignment constructor, but I am not entirely sure, please enlighten me.