Question:
# Include <iostream>
Int main ()
{
STD: cout <"this is in main" <STD: Endl;
Return 0;
}
How to make the output content of Mian () in the above Code as follows:
Start
This is in main
End
Answer:
The method I first came up with was to rewrite the cout class in STD and modify the implementation of the <operator. After interviewing my experts, it was dangerous to reload the operator, it is not realistic to rewrite all functions. At that time, I was frustrated and did not think of any other method.
When I came back, I found a word about global variables in my basic knowledge:
The global object is initialized before calling Main.
The above question can be fully realized by declaring a global variable.
The specific implementation code is as follows: add a GB class definition before main, and then create a global variable for this class:
# Include "stdafx. H"
# Include <iostream>
Class GB
{
Public:
GB: GB ()
{
STD: cout <"start" <STD: Endl;
};
GB ::~ GB ()
{
STD: cout <"end" <STD: Endl;
};
};
Gb;
Int main ()
{
STD: cout <"this is in main" <STD: Endl;
Return 0;
}