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.
-
Are you returning the "this" BaseSocket instance in the function?TheDarkKnight– TheDarkKnight2013年04月29日 13:05:38 +00:00Commented Apr 29, 2013 at 13:05
1 Answer 1
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