1

我编写了一个自定义流类,它输出缩进文本并且具有可以更改缩进级别的操纵器。所有缩进工作都在自定义流缓冲区类中实现,该流类使用该类。缓冲区正在工作(即文本在输出中缩进),但我无法让我的操纵器工作。我在很多地方都在阅读 ostream(我的类扩展)如何重载 operator<<,如下所示:

ostream& ostream::operator << ( ostream& (*op)(ostream&))

{
    // call the function passed as parameter with this stream as the argument
    return (*op)(*this);
}

这意味着它可以将函数作为参数。那么为什么我的“缩进”或“缩进”流函数没有被识别呢?我确定我必须对 operator<< 进行一些重载,但我不应该不需要吗?请参阅下面的代码:

#include <iostream>
#include <streambuf>
#include <locale>
#include <cstdio>

using namespace std;

class indentbuf: public streambuf {

public:

    indentbuf(streambuf* sbuf): m_sbuf(sbuf), m_indent(4), m_need(true) {}

    int indent() const { return m_indent; }
    void indent() { m_indent+=4; }
    void deindent() { if(m_indent >= 4) m_indent-= 4; }

protected:

    virtual int_type overflow(int_type c) {

        if (traits_type::eq_int_type(c, traits_type::eof()))

            return m_sbuf->sputc(c);

        if (m_need)
        {
            fill_n(ostreambuf_iterator<char>(m_sbuf), m_indent, ' ');
            m_need = false;
        }

        if (traits_type::eq_int_type(m_sbuf->sputc(c), traits_type::eof()))

            return traits_type::eof();

        if (traits_type::eq_int_type(c, traits_type::to_char_type('\n')))

            m_need = true;

        return traits_type::not_eof(c);
    }

    streambuf* m_sbuf;
    int m_indent;
    bool m_need;
};

class IndentStream : public ostream {
public:
    IndentStream(ostream &os) : ib(os.rdbuf()), ostream(&ib){};

    ostream& indent(ostream& stream) {
        ib.indent();
        return stream;
    }

   ostream& deindent(ostream& stream) {
        ib.deindent();
        return stream;
    }

private:
    indentbuf ib;
};

int main()
{
    IndentStream is(cout);
    is << "31 hexadecimal: " << hex << 31 << endl;
    is << "31 hexadecimal: " << hex << 31 << endl;
    is << "31 hexadecimal: " << hex << 31 << deindent << endl;
    return 0;
}

谢谢!

4

1 回答 1

4

你的操纵器应该被声明为一个只接受一个类型参数的函数ostream&。但是,如果您将其设为成员函数,您就会知道还有一个隐式this参数被传递给该函数。

因此,您应该将您的操纵器声明为免费的非成员函数,使其成为friend您的类,以便它可以访问其私有成员ib

class IndentStream : public ostream {
public:
    IndentStream(ostream &os) : ib(os.rdbuf()), ostream(&ib){};

    ostream& indent(ostream& stream) {
        ib.indent();
        return stream;
    }

    friend ostream& deindent(ostream& stream);
//  ^^^^^^

private:
    indentbuf ib;
};

ostream& deindent(ostream& stream)
{
    IndentStream* pIndentStream = dynamic_cast<IndentStream*>(&stream);
    if (pIndentStream != nullptr)
    {
        pIndentStream->ib.deindent();
    }

    return stream;
}

int main()
{
    IndentStream is(cout);
    is << "31 hexadecimal: " << hex << 31 << endl;
    is << "31 hexadecimal: " << hex << 31 << deindent << endl;
    is << "31 hexadecimal: " << hex << 31 << endl;
    return 0;
}

或者,如果您真的希望您的功能成为成员,您可以使其成为static:

class IndentStream : public ostream {
public:
    IndentStream(ostream &os) : ib(os.rdbuf()), ostream(&ib){};

    ostream& indent(ostream& stream) {
        ib.indent();
        return stream;
    }

    static ostream& deindent(ostream& stream)
    {
        IndentStream* pIndentStream = dynamic_cast<IndentStream*>(&stream);
        if (pIndentStream != nullptr)
        {
            pIndentStream->ib.deindent();
        }

        return stream;
    }

private:
    indentbuf ib;
};

但是,这将迫使您使用限定名称来引用它:

int main()
{
    IndentStream is(cout);
    is << "31 hexadecimal: " << hex << 31 << endl;
    is << "31 hexadecimal: " << hex << 31 << IndentStream::deindent << endl;
    //                                       ^^^^^^^^^^^^^^
    is << "31 hexadecimal: " << hex << 31 << endl;
    return 0;
}
于 2013-02-24T16:46:11.977 回答