inheritance - 如何从 std::ostream 继承?

标签 inheritance stl ostream

我一直在谷歌搜索,但我找不到一个简单的答案。它应该很简单,就像 STL 通常一样。

我想定义从 std::ostream 公开继承的 MyOStream。假设每次将某些内容写入我的流时,我都想调用 foo() 。

class MyOStream : public ostream {
public:
  ...
private:
   void foo() { ... }
}

我知道ostream的公共(public)接口(interface)是非虚拟的,那怎么办呢?
我希望客户能够在 MyOStream 上同时使用 operator<< 和 write() 和 put() 并使用我的类(class)的扩展能力。

最佳答案

我不知道这是否是正确的解决方案,但我以这种方式从 std::ostream 继承。它使用从 std::basic_streambuf 继承的缓冲区,一次获取 64 个字符(如果刷新则更少),并将它们发送到一个通用的 putChars() 方法,在该方法中完成数据的实际处理。它还演示了如何提供用户数据。

Live Example

#include <streambuf>
#include <ostream>
#include <iostream>

//#define DEBUG

class MyData
{
    //example data class, not used
};

class MyBuffer : public std::basic_streambuf<char, std::char_traits<char> >
{

public:

    inline MyBuffer(MyData data) :
    data(data)
    {
        setp(buf, buf + BUF_SIZE);
    }

protected:

    // This is called when buffer becomes full. If
    // buffer is not used, then this is called every
    // time when characters are put to stream.
    inline virtual int overflow(int c = Traits::eof())
    {
#ifdef DEBUG
        std::cout << "(over)";
#endif
        // Handle output
        putChars(pbase(), pptr());
        if (c != Traits::eof()) {
            char c2 = c;
            // Handle the one character that didn't fit to buffer
            putChars(&c2, &c2 + 1);
        }
        // This tells that buffer is empty again
        setp(buf, buf + BUF_SIZE);

        return c;
    }

    // This function is called when stream is flushed,
    // for example when std::endl is put to stream.
    inline virtual int sync(void)
    {
        // Handle output
        putChars(pbase(), pptr());
        // This tells that buffer is empty again
        setp(buf, buf + BUF_SIZE);
        return 0;
    }

private:

    // For EOF detection
    typedef std::char_traits<char> Traits;

    // Work in buffer mode. It is also possible to work without buffer.
    static const size_t BUF_SIZE = 64;
    char buf[BUF_SIZE];

    // This is the example userdata
    MyData data;

    // In this function, the characters are parsed.
    inline void putChars(const char* begin, const char* end){
#ifdef DEBUG
        std::cout << "(putChars(" << static_cast<const void*>(begin) <<
            "," << static_cast<const void*>(end) << "))";
#endif
        //just print to stdout for now
        for (const char* c = begin; c < end; c++){
            std::cout << *c;
        }
    }

};

class MyOStream : public std::basic_ostream< char, std::char_traits< char > >
{

public:

    inline MyOStream(MyData data) :
    std::basic_ostream< char, std::char_traits< char > >(&buf),
    buf(data)
    {
    }

private:

    MyBuffer buf;

};

int main(void)
{
    MyData data;
    MyOStream o(data);

    for (int i = 0; i < 8; i++)
        o << "hello world! ";

    o << std::endl;

    return 0;
}

关于inheritance - 如何从 std::ostream 继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/772355/

相关文章:

c++ - 如何从普通bst继承红黑树

在 CPP 文件 (Visual Studio) 中实现的 C++ 模板错误

c++ - 使用 std::map 作为关联数组

c++ - 限制 STL 的 vector max_size

java - 如何在子组件中包含标准HST组件的所有参数?

JavaScript 继承 Object.create()?

c++ - 如何迭代容器中的前十项

c++ - cout在哪里声明?

c++ - 如何对类的 const 实例使用打印运算符?

c++ - 让 C++ 类充当自定义 ostream、sstream