,
.
streambuf,
:
class CountOutput : public std::streambuf
{
std::streambuf* myDest;
std::ostream* myOwner;
int myCharCount;
protected:
virtual int overflow( int ch )
{
++ myCharCount;
return myDest->sputc( ch );
}
public:
CountOutput( std::streambuf* dest )
: myDest( dest )
, myOwner( NULL )
, myCharCount( 0 )
{
}
CountOutput( std::ostream& dest )
: myDest( dest.rdbuf() )
, myOwner( &dest )
, myCharCount( 0 )
{
myOwner->rdbuf( this );
}
~CountOutput()
{
if ( myOwner != NULL ) {
myOwner.rdbuf( myDest );
}
}
int count() const
{
return myCount;
}
};
, std::ostream:
CountOutput counter( someOStream );
int outputCount = counter.count();
,
.