User-defined Output Stream Manipulators in C

后端 未结 3 1437
一向
一向 2020-12-21 09:56

I am learning C++, and in the part of user-defined output stream manipulatior, I am stuck. This is the example code:

    #include 
     using         


        
3条回答
  •  忘掉有多难
    2020-12-21 10:37

    std::basic_ostream has several overloads of operator<<, one of which has the following signature:

    basic_ostream& operator<<( basic_ostream& st, 
                           std::basic_ostream& (*func)(std::basic_ostream&) );
    

    That is, this function takes a pointer to a function that both takes and returns std::ios_base. The method is called by this function and is incorporated into the input/output operation. Thereby making this possible:

    std::cout << endLine;
    

    So what will happen is that endLine is converted into a function pointer and a new line character will be written to the stream and afterwards a flush operation.

提交回复
热议问题