سؤال

My class looks like:

class FileOut
{
private:
    std::ofstream stream;
public:
    FileOut(string sciezka);
    ~FileOut(void);

    friend FileOut & operator<< (FileOut & obiekt, const char* w);
    friend FileOut & operator<< (FileOut & obiekt, const string & w);
    friend FileOut & operator<< (FileOut & obiekt, const char & znak);
    friend FileOut & operator<< (FileOut & obiekt, const int & liczba);
    friend FileOut & operator<< (FileOut & obiekt, const double & liczba);
    friend FileOut & operator<< (FileOut & obiekt, std::endl);
    //friend FileOut & endl (FileOut & obiekt);
};

operator<< works fine for strings, char, int etc. (I put new characters to stream). I want to implement manipulator endl for my class. Generally we override it this way:

ostream & endl (ostream & os){ return os << '\n'; }

but it won't work for my class. I declared

friend FileOut & endl (FileOut & obiekt);

but it doesn't work. How can I do it to be able to write:

FileOut save("file.txt");
save<<"tralalala"<<endl<<1123;

???

هل كانت مفيدة؟

المحلول

Since std::eol is actually a function template of the type compatible with ostream& (*fctr) (ostream&), you need to overload the following operator:

class FileOut
{
//...    
friend FileOut & operator<< (FileOut & obiekt, ostream& (*fctr) (ostream&))
//...
};

Inside the overload, you need to make sure that fctr actually matches eol (it can be eof, tab, or any manipulator matching the signature) and then write whatever it is you want to write to a file on eol:

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

using namespace std;

class FileOut
{
private:
    std::ofstream stream;
public:
    FileOut(std::string sciezka):stream(sciezka) {};
    ~FileOut(void){};

    friend FileOut & operator<< (FileOut & obiekt, const char* w) {obiekt.stream<<w;return obiekt;} ;
    friend FileOut & operator<< (FileOut & obiekt, const string & w);
    friend FileOut & operator<< (FileOut & obiekt, const char & znak);
    friend FileOut & operator<< (FileOut & obiekt, const int & liczba){obiekt.stream<<liczba;return obiekt;};
    friend FileOut & operator<< (FileOut & obiekt, const double & liczba);
    friend FileOut & operator<< (FileOut & obiekt, ostream& (*pf) (ostream&));
    //friend FileOut & endl (FileOut & obiekt);
};

FileOut & operator<< (FileOut & obiekt, ostream& (*pf) (ostream&))
{
    if(pf==&std::endl)
    {
        obiekt.stream<<"MY FAT JUICY EOL";
    }

    return obiekt;
}

int main(int argc, char* argv[])
{
    FileOut save("file.txt");

    save<<"tralalala"<<endl<<1123;
    return 0;
}

نصائح أخرى

Alternatively, if you don't like using std::eol and want your own:

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

using namespace std;

class FileOut
{
private:
    std::ofstream stream;
public:
    FileOut(std::string sciezka):stream(sciezka) {};
    ~FileOut(void){};

    friend FileOut & operator<< (FileOut & obiekt, const char* w) {obiekt.stream<<w;return obiekt;} ;
    friend FileOut & operator<< (FileOut & obiekt, const string & w);
    friend FileOut & operator<< (FileOut & obiekt, const char & znak);
    friend FileOut & operator<< (FileOut & obiekt, const int & liczba){obiekt.stream<<liczba;return obiekt;};
    friend FileOut & operator<< (FileOut & obiekt, const double & liczba);
    friend FileOut & operator<< (FileOut & obiekt, void (*pf) ());

    static void eol() {};
    //friend FileOut & endl (FileOut & obiekt);
};

FileOut & operator<< (FileOut & obiekt, void (*pf) ())
{
    if(pf == FileOut::eol)
    {
        obiekt.stream<<"MY FAT JUICY STATIC EOL";
    }

    return obiekt;
}

int main(int argc, char* argv[])
{
    FileOut save("file.txt");

    save<<"tralalala"<<FileOut::eol<<1123;
    return 0;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top