C++ puzzles

Due to my interest in low-level programming, I like to explore how the compiler implements various C++ features on the machine. Usually, understanding how the generated machine code looks allows you to utilize some undefined behaviour in C++ to do interesting, and sometimes suprising things.

When I run into something interesting, I will post it here in QA-form, which means that there is one part showing some strange behavior, and another part which tries to explain what is actually happening, and why it is probably a bad idea to do as I have done!

Usually, these problems are carefully crafted to exploit implementation defined things in such a way that the program behaves strangely rather than crashing, so a strategy for solving the puzzles is to fiddle around with the code and see what breaks and when! All code is tested on the Visual Studio 2008 compiler, as well as a recent version of gcc. Try out others as well, in most cases it will work!

Puzzles

1: Forward declarations
2: Wrong function called
3: Reference counting trouble
4: Dynamic function calls
5: Exploring function calls
6: Broken container
7: Public read-only data
8: Avoiding const_cast
9: Exceptions and inheritance
10: Tricky booleans