-2

I was just asking if you can create your own cout<< like object in C++. most people confuse my question with operator overloading <<. But no, i dont want to implement my own << operator so that when users print my object i can control what they get. But basically i want to implement like this:

something << some_given << some_end; 

Not sure if that is possible , but the iostream standard library created the cout , so my mind says "Why not?". So i asked stackoverflow. Help would be appreciated! :)

7
  • 1
    I have no idea what you are asking. Commented Jul 25, 2016 at 3:21
  • What is something in this case? Commented Jul 25, 2016 at 3:22
  • I think op wants to make a cout object, but not cout. I think op understands about how to use operator<< on different objects (e.g. output to file etc), but wants to make a cout that isn't cout. Commented Jul 25, 2016 at 3:22
  • 1
    Are you asking how to implement your own stream handler? Commented Jul 25, 2016 at 3:22
  • yes @kfsone thanks! I was looking for that Commented Jul 25, 2016 at 3:23

1 Answer 1

3

I'm not sure if I interpretted your question correctly but I think you want a class with an overloaded operator<< so that's what i have here

class MyClass { public: MyClass() = default; MyClass& operator<<(int input) { //do something with input return *this; } } 

You would use it like this;

MyClass myObject; myObject << 42; //the function would have been called 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks thats what i wanted!
best way is to make operator << return reference, like this: MyClass &operator << (...); so you would be able to write several << in one line: myObject << 42 << 37 << 14;.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.