Like most friends, I have encountered COUT to see the first C program - classic "Hello, World!", One of the best programs I have prepared by I have prepared (^ _ ^), It is probably like this: #include
Standard library designers have already customized iostream :: Operator << For the overloaded version of various C basic data types, this has enabled our beginners to enjoy Cout << "Hello, World ! "<< Endl; simple - etc, this line is connected by two <<" Hello, World "and" endl "operator, then our second Edition Hello, World! It seems to be written : Cout.operator << ("Hello, World!"). Operator << (endl); "strong equivalent". Can you write this way? Confirm to the compiler ... OK, No quoblem! Well, we have basically seen the essence of COUT, now you may wish to move your hands, you will realize a simplified version of a COUT (Lite), in order to distinguish, we name the cout object named by our design, Myout object belongs Myoutstream. What we have to do is to overload a series of different types of Operator << operators for MyoutStream classes, simply, here we only have overloaded for integer (int) and string type (char *).
In order to indicate the relationship with the iostream, we no longer use the header file iostream, and the Printf function in the old stdio is used in the old STDIO. The program is simple, including the complete main function, all as follows: #include
The comments in the program indicate that we should pay special attention to the two: ie the Operator << function is executed, always returns a reference to its own, the output has been completed, why do you want more? I still remember that a little strange cout.operator << ("Hello, World!"). Operator << (endl)? It can achieve we can write COUT << "Hello, World! << Endl; not COUT <<" Hello, World! "; Cout << Endl; why can it connect? We analyze: According to the order of execution, the system first calls Cout.operator << ("Hello, World!"), Then? Then Cout.operator << will return it itself, that is, the last line of the function will appear similar to Return * this, so Cout.operator << ("Hello, World!") Is returned to Cout Then, then it is followed by .Operator << (endl), which is equivalent to cout.operator << (endl) - then will carry out the next output, if there are many << operators, calling Will it be going on ... Wow, is it very clever? Now you understand our myoutstream :: Operator << The last row of the mystery! Note that the most exciting line in the main function: myout << mystr << a << "/ n"; we know that the last "/ n" can achieve a wrap, but we are tutorial when using C Always intentionally, let us use endl, both seem to be the same - what is the mystery? In the book, the book says ENDL is a manipulator, which not only implements the wrap operation, but also refreshes the output buffer. What does that mean? It turns out that the data is not immediately transmitted to the output device, but the first buffer is entered, and when the appropriate timing (such as the device is idle), it can be incorporated by the buffer, or forced to refresh by the manipulator Flush: Cout << "Hello, World!" << "Flush The screen !!!" << flush; so when the program executes to operator << (flash), there is a possibility that the string data in front is still in the buffer Not displayed on the screen, but after executing Operator << (flash), the program will force all the data of the buffer to the output device and empty it. And the manipulator ENDL is equivalent to the shorter version of << "/ n" << Flush, which first outputs a newline character, and then realize the refresh of the buffer. It is probably because the general output is ended in the end of the wrap, and the end is a habit of refreshing. It is convenient to combine the two into ENDL. If the reader is interested, go back to implement a similar MyoutStream to our MyoutStream, which is related to the refresh C function is FFLUSH.