作者ric2k1 (Ric)
看板EE_DSnP
標題[討論] 星期三關於 IO manipulator 卡彈的解釋
時間Fri Jan 9 23:32:39 2009
就是那天腦筋其實有點不清楚, 所以忽然忘記為什麼 IO manipulator 是那樣寫,
還好下課時有同學來提出他的看法, 回家想想後就瞭了, 所以 PO 出來跟大家分享一下:
問題: IO manipulator 到底是什麼? 為什麼自訂的 IO manipulator 的 prototype 是
ostream& myManipulator(ostream&):
想法: 原來 IO manipulator 是 function pointer, 而且不是隨便的 function
pointer 都可以哦! ostream 裡面宣告了一個 global function:
ostream& operator << (ostream& os, ostream& (*f)(ostream&))
{ // doing sometheing like:
return (*f)(os);
}
所以可以吃進去 IO manipulator, 而且可以讓使用者自訂型態符合的
function pointer 當作 IO manipulator. 至於其他型態的 function, 因為
沒有定義, 所以 by default 不行, 但是你也可以自訂一個類似的 global
function 來惡搞一下哦:
ostream& operator << (ostream&,
ostream& (*) (ostream&, const myClass&));
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.46.74