Saturday, April 28, 2012

8.15

Suppose we used the following C++ code that attempts to avoid the use of a static_cast to print out the internal value of true( see code at end of Sec 8.7)


union
{
    int i;
    bool b;
} x;
...
x.b = true;
cout << x.i << endl;


Why is this wrong(Hint: try assignment x.i = 20000 before x.b = true)


x.i is not initialized, and so will return garbage

No comments:

Post a Comment