std::filebuf
カスタム実装を使用したいSTLPort5.2.1を使用するVisualStudio2008 C++03アプリケーションがあります。例えば:
class MyFileBuf : public std::filebuf
{
protected:
virtual int_type sync()
{
// breakpoint here never fires
return std::filebuf::sync();
};
virtual std::streamsize xsputn( const char_type* p, std::streamsize n )
{
// breakpoint here never fires
return std::filebuf::xsputn( p, n );
};
virtual int_type overflow( int_type c = traits_type::eof() )
{
// breakpoint here never fires
return std::filebuf::overflow( c );
};
};
class MyFileStream : public std::ofstream
{
public:
MyFileStream() : std::ofstream( new MyFileBuf() ) { clear(); };
~MyFileStream() { delete rdbuf(); };
};
int main()
{
MyFileStream fs;
fs.open( "test.txt" );
fs << "this is a test" << std::endl;
return 0;
}
残念ながら、MyFileBufのメンバーは誰も呼び出されません。コードをステップスルーすると、<<
オペレーターが次の場所に移動することがわかります。
stlpd_std::basic_streambuf<char,stlpd_std::char_traits<char> >::xsputn(const char* __s, long int __n)
stlpd_std::basic_streambuf<char,stlpd_std::char_traits<char> >::sputn(const char* __s, long int __n)
stlpd_std::basic_ostream<char,stlpd_std::char_traits<char> >::_M_put_nowiden(const char* __s)
stlpd_std::operator<<<stlpd_std::char_traits<char> >(stlpd_std::basic_ostream<char,stlpd_std::char_traits<char> >& , const char* __s )
main()
コールスタックの最上位は次のようになります。
MyFileBuf::xsputn(const char* p, long int n)
ただし、ファイルは正しく書き込まれます。誰かが私がどこで間違っているのかを理解するのを手伝ってくれますか?