Jumping Past Initialization

Jumping Past Initialization - int main() { goto xxx; { int...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Jumping Past Initialization As we've seen in several examples in previous newsletters, C++ does much more with initializing objects than C does. For example, class objects have constructors, and global objects can have general initializers that cannot be evaluated at compile time. Another difference between C and C++ is the restriction C++ places on transferring control past an initialization. For example, the following is valid C but invalid C++: #include <stdio.h>
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int main() { goto xxx; { int x = 0; xxx: printf(&quot;%d\n&quot;, x); } return 0; } With one compiler, compiling and executing this program as C code results in a value of 512 being printed, that is, garbage is output. Thus the restriction makes sense. The use of goto statements is best avoided except in carefully structured situations such as jumping to the end of a block. Jumping over initializations can also occur with switch/case statements....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Ask a homework question - tutors are online