operator overloading - Have a C++ Class act like a custom ostream, sstream -


I have a C ++ class MyObject and I want to be able to feed this data As I do for an ostream (but unlike a direct stream, incoming data is formatted in a special way) I can not know how to load an operator for input given to MyObject To do.

class MyObject {public: ostringstream s; Feed me}; Int main () {MyObject obj; Obj.FEEDME & lt; & Lt; "Hello" & lt; & Lt; 12345; // I want obj.s == ": Hello :: 12345:"}

I would like to feed each item like this:

Then in the given example , S = ": Hello :: 12345" The end result should be. What is my question, how can I tell that object whenever & lt; & Lt; Something , put: something around:

Is this possible?

Try this:

  class MyObject {public: template  ; Class T & gt; My Object and Operator & lt; & Lt; (Constant & amp; nd) {s & lt; & Lt; ':' & Lt; & Lt; X & LT; & Lt; ':'; Return * This; } Std :: string to_string () const {return s.str ()); } Private: std :: ostringstream s; }; MyObject obj; Obj & lt; & Lt; "Hello" & lt; & Lt; 12345; Std :: cout & lt; & Lt; Obj.to_string () & lt; & Lt; Std :: endl; There are some things that you will not be able to penetrate in the stream, but it should work for all basic things. 


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -