I'm currently working on a socket program.
Is it possible to use the stream insertion operator as in the use case below ?
__sin >> var; // similar to cin >> var;
where the body of the function is something like
var = remoteConsole.getline();
so i can use the function in place of cin when im working with my remote console, in a remote location I know i can use something like
cin.getLine()
or var= remoteConsole.cin()
but im trying to avoid that.
Im starting to think that what im trying to do is impossible though it seems easy, i may have to dump the idea. I've had a good look around but everything i try is littered with compile errors. any help would be appreciated. Thank you
EDIT
for those who were unclear as to what i was talking about (which i tryed to clearly explain) i was trying to make a function with the same syntax as cin but instead of getting the input from a console, getting it from somewhere else, ie a socket. i was having trouble creating a function that took << or >> instead of ( and ). i wanted the syntax to be the same as cin >> and cout <<. my question should have been, where do i start on the road to doing this?.
Ive found out a way to do it, but im not shure how safe it will be. I created two struct's and overloaded the << in one and >> in the other
struct out_t {
template<typename T>
out_t& operator << (T&& x) {
// Send x to the socket
// Use console for now
cout << x;
return *this;
}
};
out_t socout;
struct in_t {
template<typename T>
in_t& operator >> (T&& x) {
// get x from the socket and wait untill done so;
// Use console for now
cin >> x;
return *this;
}
};
in_t socin;
so now i can use
socout << "Some message"; // which i can now send to an external console
socin >> strValue;// i can request the data needed from external program/computer
sorry if i didn't explain this well to start with. there may be room for improvements but im quite happy with this. if this is not the way to go, could somebody please please advise me of the proper way.
Thanks all;