The following code never prints out the counter, assuming the compiler
does something intelligent to initialize int data member types to
zeros?
The assumption is wrong. The value of an uninitialized variable is indeterminate. It could be some random number that just happens to be there in memory.
Change the constructor line to this:
A(): b(0), a(0) { }
This explicitly initializes the values to zero, and also communicates intent clearly. Remember that code is there for humans to read. The computer never actually sees your C++ code.
Also note that the following is equivalent:
A(): b(), a() { }
although I don't personally use this method of initialization.
The initialization is not the only problem with the code. For one, you have huge memory leaks in the code, as you don't delete
the instances of A
you new
ed. Go pick up a good introductory C++ book and learn proper modern C++, as your post demonstrates that you don't quite have a grasp of the language fundamentals.