{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Jumping Past Initialization

Jumping Past Initialization - int main goto xxx int x = 0...

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("%d\n", 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

{[ snackBarMessage ]}

Ask a homework question - tutors are online