我们在最初学习C++语言时就接触到"cout<<endl;"这样的语句。我们都知道endl表示换行的意思。我一直习惯这样使用,却并不是清楚其内部实现是怎么样的。今天看《C++ Standard Library, Sec. 13.6.1》,才真正弄明白。
首先,endl是一个操作符(Manipulators),但我们必须知道endl是一个什么类型的变量。endl是跟在”<<“运算符后面,故endl应该是一个参数。其实endl是一个函数名,它是一个"<<"运算符重载函数中的参数,参数类型为函数指针。下面我们看下内部函数实现。
1 ostream& ostream::operator << ( ostream& (*op) (ostream&)) 2 { 3 // call the function passed as parameter with this stream as the argument 4 return (*op) (*this); 5 }
1 std::ostream& std::endl (std::ostream& strm) 2 { 3 // write newline 4 strm.put('\n'); 5 // flush the output buffer 6 strm.flush(); 7 // return strm to allow chaining 8 return strm; 9 }
这样我们知道在标准库中endl是作为一个函数实现的,显然我们也可以直接调用这一函数。我们看下面的测试程序: 1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 cout<<endl; 7 endl(cout); 8 return 0; 9 }
|
|