os.pword (index)方法应该返回由自定义操纵器指定的当前日期格式。但是, pword()的结果会被不同的流通过它的str(string)方法修改。
ostringstream os;
istringstream is;
CDate f ( 2000, 5, 12 );
os << date_format ( "%Y-%m-%d" ) << f;
is.clear ();
is.str ( "05.06.2003" ); // causes pword() to return "05.06.2003" instead of "%Y-%m-%d"
os.str ("");
os << f;
类方法:
struct date_format
{
static const int index;
string format;
explicit date_format(const char * fmt)
{
format = string(fmt);
}
friend ostream & operator<<(ostream & s, const date_format & df)
{
s.pword(index) = (void*)df.format.c_str();
return s;
}
};
const int date_format::index = ios_base::xalloc();
class CDate
{
...
friend ostream & operator<<(ostream & os, const CDate & cdate)
{
/* should point to the current date format of os */
const char * ptr = (const char*)os.pword(date_format::index);
...
return os;
}
}
是什么导致了这种行为,可以采取哪些措施来避免这种行为?