Accessing overloaded operator through pointer

I'm using the tcp::iostream from boost to send a message to a server, but i'm getting a weird issue where my program sends out a memory address instead of the string that I actually want to send. For example, if I output "test string", what my program actually sends out is something like "4EB6A4".

However, this only happens if I access the tcp::iostream's << operator through a pointer. The following works fine...

1
2
		tcp::iostream conn(server, port);
		conn << "test message" << endl; //outputs "test message" 


This does not...

1
2
		conn = new tcp::iostream(server, port);
		conn->operator << ("test message") << endl; //outputs a memory address (e.g. "4EB6A4") 


Does anyone know what I'm doing wrong?
Last edited on
Somehow it seems to work when use the insertion operator with "*conn" instead of "conn->operator<<".

1
2
		conn = new tcp::iostream(server, port);
		*conn << ("test message") << endl;


Could someone explain why "conn->operator <<" was behaving differently?
Due to the syntax of the way you're calling it:
conn->operator << must call a member function.

*conn << , on the other hand, can call a member function or a global operator overload depending on whichever is the closest match for the given parameters.

Here, the << operator you want is probably a global function and not a member function. So using conn->operator << syntax can't find the right overload.
Last edited on
Topic archived. No new replies allowed.