Tutorial :How do I overload the << operator?



Question:

I intend to call a function whenever m_logger<<"hello"<<"world" is called. m_logger is of type ofstream.

So i decide to overload << with following signature

friend ofstream& operator<<(ofstream &stream,char *str);  

However the vc compiler gives following error:

error C2666: 'operator <<' : 6 overloads have similar conversions

Is there anyother way to achieve this, my objective is to divert all the write operation to ofstream object to different function?

Creating an object of my own calss works for me, however how can i make it work like normal ofstream object which typecasts all system defined types into strings or char*. i know one approach would be to overload the operator for each and every type but is there a cleaner approach


Solution:1

The problem is that ofstream is already overloaded this way. If you make mlogger of a new type holding an ofstream, then you can do this:

class mlogger_t {  public:      ofstream stream;      ...  }    mlogger_t& operator<<(mlogger_t& stream, const string& str) {      stream.stream << str;      ...  }    //EDIT: here is how to make this work for other types too using templates:  template<typename T> mlogger_t& operator<<(mlogger_t& stream, T val) {      stream.stream << val;  }    ...    mlogger_t mlogger;    mlogger << "foo";  

Also, you should definitely use a const string& (as I did in this example) rather than a C-style string. If you really need it to be C-style, at least use const char *.


Solution:2

"overload" isn't "override". You can overload a function or operator for arguments of different types; you cannot override an existing function or operator with your own implementation (aside from overriding virtual functions, which is obviously very different). The only exceptions are operator new and operator delete, where it's possible to override built-in ones.


Solution:3

You could change the type of the m_logger object.


Solution:4

Depending on why you want to overload operator<<, the correct solution is either

  • to use another type than a descendant of ostream as target stream; in that case you have to write all operators << yourself, but you can get help from templates if you want to forward by default.

Like this:

template <typename T>  myStream& operator<<(myStream& s, T const& v)  {      s.getStream() << v;  }  

and you'll see that manipulators don't match the template, so you'll also need something like:

myStream& operator<<(myStream& fl, std::ostream& (*fn)(std::ostream&))  {      s.getStream() << fn;  }  
  • to write your own streambuf which delegates I/O to a std::filebuf (this is a little too complicated to give an example here, search the web -- filtering streambuf is a good keyword for that. If I remember correctly, boost has an helper library for that which may be useful. Note that in this case, you'll probably end up by using another type that an fstream, but which will descend from ostream.


Solution:5

What you should do is make a class and then define operator<<. An operator overload must contain at least one user-defined type. Similarly, you can't write a new operator+(int, int).


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »