Hallo, I'm a total newbie in the C++ world. Started to learn it a month ago.
So far I've been using Fortran, where to print out some data to many different output files it was enough to create a function that would do exactly that.
1 2 3 4 5
function printtofiles(data)
write(1,*) data
write(2,*) data
write(3,*) data
end function
I know that in C++ I can do the same, but since I decided to write the code in an object oriented language, I wanted to start using classes and operators and all those goodies that C++ comes with. Here is what I have in mind.
I wanted to create a class and an operator like << so that having in the code printtofiles << "some text";
would do exactly that, just like cout << "some text"
prints out "some text" to the screen.
Yes I have, and I know how to write to files with <fstream> library. I don't know how to make such a class. I'm guessing that it has be derived from this class but I don't know how to do that.
OK, this is close. But what if I want to printout some data not a string. I know I can first place everything in a string and then use that. But a more convenient way to use it would be to writetofiles << "Sample text" << variable << 18274 << endl;
And this is what I'm after.
You'd have to overload the << operator like this: writeOut& operator << (writeOut& out, const std::string& str)
For everything else you want to print out instead of std::string. I'd suggest you to overload the << operator for builtin types and std::string.
Theoretically you could also do it for an own type that you could call WTF_Printable or something like that. That would be an abstract class that only has the abstract function toString(), so it would look like that:
1 2 3 4 5
class WTF_Printable
{
public:
virtual std::string& toString() = 0;
}
Other printable datatypes would have to inherit that type and implement toString to become printable.
(Though I personally think that's pretty pointless- if the class already implements toString, you could just call toString() and pass the string to the function, without needing to worry about implementing an interface).
Overloading << operator for builtin types works. But how to overload this operator to use it with standard manipulators like endl or fixed or scientific, so that I could format my output stream.
I'd like this class to have similar functionality that cout does.
For that you'd have to look at how the << operators in the STL stream classes work. I think they were just function pointers, so you'd have to overload the << operator to accept function pointers (of parameterless functions, functions with parameters must be done otherwise)
Actually, you'd have to replace std::ostream with whatever class or struct you're using. You can't pass an ostream to your functions there, and you can't really work with the returned ostream either.
Sorry, I don't see it. ostream& endl ( ostream& os ); That is how endl is declared (and I'm assuming that the others manipulators too).
The function recieves a manipulator and returns a reference to a writeOut.
The typedef is just syntactic sugar
Edit: that manipulator will be applied to every ofstream in the container.