2014-10-15 4 views
0

나는 표준 : IStream을 서브 클래스를 쓰고 있어요 :: stringbuf 서브 클래스표준 : : IStream을 서브 클래스 :이 유효 자신의 표준을 사용 소멸자

class decostream : public std::istream 
{ 
public: 
    decostream(std::istream * input) 
     : std::istream(new decostreambuf(input)) 
     { 
     } 

    ~decostream() { delete rdbuf(); } 
} 

에 streambuf의 삭제인가? std :: istream 문서에서 istream이 파기되었을 때 streambuf가 여전히 유효해야 하는지를 아는 것은 어렵습니다.

+0

유효합니까? 예. 교활한? 당근 빠따 지. 대신에 buf를'decostream'의 멤버로 만드십시오. 어떤 것이 제거되거나 연결된 버퍼를 변경하면 버퍼가 누출 될 수 있으며, 소유하지 않은 버퍼를 삭제할 수도 있습니다 (치명적인). 이 모든 것은'decostringstream'이'streambuf'에서 파생되었다고 가정합니다. 왜 혼동스러운 이름입니까? – user657267

+0

@ user657267이 옳다고 생각합니다. 적어도이 대답은 http://stdcxx.apache.org/doc/stdlibug/39-3.html에서 제안하는 내용입니다 (http://stackoverflow.com/a/6490625/1133179). – luk32

+0

혼란스러운 이름으로 불편을 끼쳐 드려 죄송합니다. 간략한 예를 쓸 때 이것은 실수였습니다. 이제 해결되었습니다. – galinette

답변

0
#include <istream> 
#include <sstream> 

class decostreambuf : public std::stringbuf { 
public: 
    decostreambuf(std::istream* input) { 
     //... 
    } 
}; 

class decostream : public std::istream { 
public: 
    decostream(std::istream* input) : sb(input), std::istream(&sb) { 
     //... 
    } 
private: 
    decostreambuf sb; 
};