ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c-为自定义流类编写操纵器

2019-10-13 16:06:46  阅读:217  来源: 互联网

标签:c operator-overloading iostream streambuf


我编写了一个自定义的流类,该类输出缩进的文本,并且具有可以更改缩进级别的操纵器.所有缩进工作都是在自定义流缓冲区类中实现的,该类由流类使用.缓冲区正在工作(即,文本在输出中缩进了),但是我无法使操纵器工作.我在很多地方都读到ostream(我的课程扩展了)如何使运算符超载<<像这样:

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;
}

谢谢!

解决方法:

您的操纵器应声明为仅接受ostream&类型的一个参数的函数.但是,如果将其设为成员函数,则您也知道该参数也隐式地传递给函数.

因此,您应该将操纵器声明为一个免费的非成员函数,使其成为您的类的朋友,以便它可以访问其私有成员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;
}

另外,如果您确实希望函数成为成员,则可以将其设为静态:

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;
}

标签:c,operator-overloading,iostream,streambuf
来源: https://codeday.me/bug/20191013/1909035.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有