Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am learning boost library for the network programming and i read about boost bind and have some doubts in the following code.

VB
boost::asio::async_read_until(socket_, buf, boost::regex("\r\n\r\n"),
          boost::bind(&connection::handle_read, shared_from_this(),
                      ba::placeholders::error,
                      ba::placeholders::bytes_transferred));


here inside bind it has three arguments
VB
(shared_from_this(),
                      ba::placeholders::error,
                      ba::placeholders::bytes_transferred)


but the member function "handle_read" inside the "connection" class has only two arguments as given below.

C++
void handle_read(const boost::system::error_code& error,
                    size_t bytes_transferred) {
       ba::async_write(socket_, ba::buffer(message_),
                       boost::bind(&connection::handle_write, shared_from_this(),
                                   ba::placeholders::error,
                                   ba::placeholders::bytes_transferred));
   }


i dont understand why bind uses an extra argument shared_from_this(),i also want to know about the placeholders.
can any one please clarify on this .


with regards,
hari .
Posted

1 solution

Because that is the way the designer wrote the function; note that boost::bind() actually takes four parameters in both cases. You need to check the documentation to see the significance of each parameter in a function.
 
Share this answer
 
Comments
hariharansa 26-Oct-11 8:44am    
but boost::bind() will bind the arguments to the functions it has address.here it has the address to connection::handle_write and that method of the class has only two arguments , but i am still now confused with the shard_from_this,
is it possible to expalin a little more on this or any tutorial is enough..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900