0
사용자 정의 std::filebuf
구현을 사용하고자하는 STLPort 5.2.1을 사용하는 Visual Studio 2008 C++ 03 응용 프로그램이 있습니다. 예 :어떻게 std :: filebuf를 재정의합니까?
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의 구성원 중 누구도 호출되지 않습니다.
MyFileBuf::xsputn(const char* p, long int n)
파일은, 그러나, 제대로 기록됩니다 내가 코드를 단계별로하면 내가 호출 스택의 상단이있을 것으로 예상 할 경우, 나는 <<
운영자가
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()
에가는 것을 알 수있다. 아무도 나를 잘못 이해할 수 있습니까?
내가 컴파일하는 것이 놀랍 :
이 솔루션은 다음과 같이 나타납니다. 'ofstream'에는'streambuf *'인수를 취하는 생성자가 없습니다. ofstream에서 MyFileStream을 파생시킬 아무런 이유가 없다. 단지 ostream 만 괜찮을 것이다. 'filebuf'에서'MyFileBuf'를 파생시키는 이유를 실제로 볼 수 없습니다. 다시'streambuf'는 정상입니다. 아마 당신이 왜 이런 식으로하려고하는지 설명하면 도움이 될 것입니다. – jahhaj
실제로, 'ofstream'은 그런 식으로 구성 될 수 없습니다. 일부 STL 라이브러리는 "확장"기능을 제공합니다.이 확장 기능은 결국이 기능과 같이 예상치 못한 경우에 다시 돌아옵니다. 이 상황에서 생성자 내부에서'init (new MyFileBuf);를 호출하면 초기화가 제대로 수행되어야합니다. – DanielKO
@DanielKO - 네 말이 맞아. 나는 의도하지 않게 STLPort 확장을 사용하고있었습니다. 내가 그걸 끄면 jahhaj가 예측 한 컴파일 오류가 발생했습니다. – PaulH