大多数情况下,当人们想要将其类提供给像friend这样的ostream对象时,人们通过将其设为cout来使其重载。但是对我而言并非如此。我需要operator<<的重载,可以让我做这样的事情:

std::fstream fileobj(".\example.file", std::ios::in | std::ios::binary);
AraHaan::hexstream hexstrm;
hexstrm << fileobj; //<--  itterates through the file object and hex arrays the data in it.


问题是我不确定如何做到这一点。 hexstream的当前Class代码如下:

#ifndef HEXSTREAM_DEFINED
#define HEXSTREAM_DEFINED
#include "basic_hexstream"

namespace AraHaan {
    class hexstream: public AraHaan::basic_hexstream {
    };
}

#endif


basic_hexstream

#ifndef BASIC_HEXSTREAM_DEFINED
#define BASIC_HEXSTREAM_DEFINED
#include <sstream>
#include <iomanip>

namespace AraHaan {
    class basic_hexstream {
    private:
        std::stringstream base_hexstream;
        bool data_cleared;
        bool append0x, writehexseparater;
    public:
        void AddCharacter(int character) {
            data_cleared = false;
            if (append0x) {
                if (writehexseparater) {
                    base_hexstream << "0x" << std::uppercase << std::setfill('0') <<
                        std::setw(2) << std::hex << static_cast<unsigned short>(character) << ", ";
                } else {
                    base_hexstream << "0x" << std::uppercase << std::setfill('0') <<
                        std::setw(2) << std::hex << static_cast<unsigned short>(character);
            }
            } else {
                if (writehexseparater) {
                    base_hexstream << std::uppercase << std::setfill('0') <<
                        std::setw(2) << std::hex << static_cast<unsigned short>(character) << ", ";
                } else {
                    base_hexstream << std::uppercase << std::setfill('0') <<
                        std::setw(2) << std::hex << static_cast<unsigned short>(character);
                }
            }
        }
        void setoptions(bool append_0x, bool writehexseparator) {
            append0x = append_0x;
            writehexseparater = writehexseparator;
        }
        const std::string str() {
            return base_hexstream.str();
        }
        void clear() {
            data_cleared = true;
            base_hexstream.clear();
            base_hexstream.str("");
        }
        /*
        TODO: make this work for ifstream, fstream, and FILE* file objects.
        ex.
            std::fstream fileobj(".\example.file", std::ios::in | std::ios::binary);
            AraHaan::hexstream hexstrm;
            hexstrm << fileobj; //<  itterates through the file object and hex arrays the data in it.
        */
        void operator<< (void* cool) {}
        void operator<< (int character) {
            // Note: Clearing the hexstream after every character is the user's responsibility
            //     if ran in a for loop that can dublicate the information that is if the data from
            //     the hexstream is obtained and added to a string after every itteration.
            AddCharacter(character);
        }
        basic_hexstream() {}
        ~basic_hexstream() {
            // clears the data just in case.
            // This makes clearing this manually optional.
            if(!data_cleared) {
                clear();
            }
        }
    };
}

#endif


但是,是的,我需要在basic_hexstream类中对此正确实现operator 的字符时正常工作。
我如何使用运算符类,并且还完成我需要它做的所有事情?

最佳答案

您可以使用其中一种方法。在下面,您有2个简单的流示例,这些流从fstream中获取数据并将其输出到控制台中。

示例1(operator <

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class TextStream
{
public:
    string getText() const;
    TextStream& operator<<(fstream& stream);

private:
    string text;
};

string TextStream::getText() const
{
    return text;
}

TextStream& TextStream::operator<<(fstream& stream)
{
    while (true)
    {
        string line;
        if (!std::getline(stream, line)) break;
        line += '\n';
        text += line;
    }

    return *this;
}

int main(int argc, char **argv)
{
    fstream file1("temp1.txt");
    fstream file2("temp2.txt");

    TextStream ts;
    ts << file1 << file2;
    cout << ts.getText() << endl;

    file1.close();
    file2.close();

    return 0;
}


示例2(operator <
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class TextStream
{
public:
    string getText() const;
    void append(const string& s);

    friend TextStream& operator<<(TextStream& textStream, fstream& fileStream);

private:
    string text;
};

string TextStream::getText() const
{
    return text;
}

void TextStream::append(const string& s)
{
    text += s;
}

TextStream& operator<<(TextStream& textStream, fstream& fileStream)
{
    while (true)
    {
        string line;
        if (!std::getline(fileStream, line)) break;
        line += '\n';
        textStream.append(line);
    }

    return textStream;
}

int main(int argc, char **argv)
{
    fstream file1("temp1.txt");
    fstream file2("temp2.txt");

    TextStream ts;
    ts << file1 << file2;
    cout << ts.getText() << endl;

    file1.close();
    file2.close();

    return 0;
}

关于c++ - 如何使用operator <<使用<<将文件对象提供给类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42462652/

10-11 23:13
查看更多