张楠自己的答案,最初发布这一问题的一部分:
追问:好的,这是实现所需的功能派生的std ::流缓冲:
class listboxstreambuf : public std::streambuf {
public:
explicit listboxstreambuf(CHScrollListBox &box, std::size_t buff_sz = 256) :
Scrollbox_(box), buffer_(buff_sz+1) {
char *base = &buffer_.front();
//set putbase pointer and endput pointer
setp(base, base + buff_sz);
}
protected:
bool Output2ListBox() {
std::ptrdiff_t n = pptr() - pbase();
std::string temp;
temp.assign(pbase(), n);
pbump(-n);
int i = Scrollbox_.AddString(temp.c_str());
Scrollbox_.SetTopIndex(i);
return true;
}
private:
int sync() {
return Output2ListBox()? 0:-1;
}
//copying not allowed.
listboxstreambuf(const listboxstreambuf &);
listboxstreambuf &operator=(const listboxstreambuf &);
CHScrollListBox &Scrollbox_;
std::vector<char> buffer_;
};
要使用这个类只创建一个std :: ostream的和这个缓冲
std::ostream os(new listboxstreambuf(some_list_box_object));
为什么不使用通用'的std :: ostream'接口和改变底层'STRE初始化ambuf'根据你的情况?不需要虚拟功能。 – mavam 2012-04-24 16:13:28
您不能使非会员功能变为虚拟。 'operator <<'不是'basic_ostream'的成员,而是一个自由函数。 – 2012-04-24 16:14:56
@MatthiasVallentin:但我需要将基类std :: ostream引用传递给我的类。在这种情况下,我从ostream的<<运算符派生的类将永远不会被调用。 – 2012-04-24 16:25:09