我正在尝试理解我设法通过反复试验来完成工作的代码段.我理解这个片段的一切,除了为什么当我把“朋友”带出课堂宣言时它不起作用.在这种情况下,我不明白朋友在做什么.
stringstream log;class logWrapper { friend ostream& operator<<(ostream& os,logWrapper& thislogend) { stringstream &ss = dynamic_cast(os); // This line replaced with printf for clarity // The actual code sends the C style string to a // legacy logging system that only takes C style strings // _log(LOG_ERR,"%s",ss.str().c_str()); printf("%s\n",ss.str().c_str());
ss.str(""); return os; } } logend; int main(void) { log << "This is a test" << logend; }