0

Well this question is not specifically about sockets, but more a C++ question. However, I have a class which can send and receive data via a socket. I would like to send data with the stream syntax, so I create the << operator in my class:

virtual const BaseSocket& operator << (const std::string &oMessage);

Now in my code and I can use it and it works:

socket << "data";

The problem I'm facing now is that I can only put a single element per line and not use:

socket << "data1" << "data2";

I'm aware that I would have to overload the operator for other datatypes when I want to stream them as well, like int, float, etc. but I couldn't find out why I can not chain the variables.

asked Apr 29, 2013 at 13:03
1
  • Are you returning the "this" BaseSocket instance in the function? Commented Apr 29, 2013 at 13:05

1 Answer 1

3

you should remove the const from const BaseSocket&

BaseSocket& operator << (const std::string &oMessage);

const is what prevents you from chaining, because the operator << itself is a non-const, so it can't be called on a const variable which is returned from the previous operator call

answered Apr 29, 2013 at 13:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.