I have used various C/C ++ ides, and some of them are really tough. For example, if Dev-C ++ is used (you can't remember whether it is, as if vs is also the case), compile the console program, debug and run the program, and the console window will flash when the program is executed, our eyes are strongly tested. Normally, we will add a blocking statement to the end, for example:
// Version 1
// # Include <conio. h>
Getch ();
// Version 4
// # Include <stdlib. h>
System ("pause ");
// Version 3
// In C ++, it can also be
// # Include <iostream>
Char chnothing;
STD: CIN> chnothing;
// Version 2
// # Include <stdio. h>
Getchar ();
These versions are commonly used. Generally, they are placed at the end of the main function, before the return statement. The result is that the execution of the program is blocked. The program will not end until we enter a character or press any key. Before that, we have enough time to view the program execution results. However, these usage methods have disadvantages:
1) For version 1, since conio. H is not a standard header file, not all ides will attach it. It seems that Dev-C ++ is not included.
1) For versions 1, 2, and 3, if too many characters are accidentally entered during program execution, our blocking line is easily broken through;
2) For objects defined in the main function, normally their destructor are called only after the main function returns. These blocking statements are called in the main function, so it is difficult for us to observe the execution result of the destructor, or even think that the Destructor is not called.
One way to solve the above problem is to execute a blocking code after the main function is executed, which requires the atexit function. We will use atexit to register a function that is called only after the end of main, and call version 4 in this function.
//
// Let the atexit function Deal With console programs that flash through .//
// I think you 'd better find an IDE that will flash into executing the console program to test the following code.
// You Need To comment out the last atexit function call statement to see what is different from the original one. // You can try it on Dev-C ++, although I haven't used it for a long time. you can also try it on vs. // I just tried it with vs2008. C-free won't flash off, if you use it. // if you use codeblocks, try again. Okay, you are serving as my mouse .//
# Include <cstdlib>
# Include <iostream> //
// The function called after the main function is executed.
//
Void calledaftermain (void)
{
// Our version 4.
System ("pause ");
}//
// The class used for testing.
// We Will instantiate an object in the main function.
//
Class clx
{
Public:
Clx (void)
{STD: cout <"constructor" <STD: Endl ;}
~ Clx (void)
{STD: cout <"destructor" <STD: Endl ;}
}; Int main (void)
{
//
// Test object.
// Observe its output in the console.
//
Clx objtest;
//
// Other statements...
//
//// Comment out the atexit function call statement at the next execution to see what is different .//
Atexit (calledaftermain );
Return exit_success;
}
Ha, the usage of the atexit function is good!