绑定函数中 class 名称前的 C++ 符号

C++ ampersand before class name in bind function

我正在研究 boost 库并卡在这一行:

boost::bind(&Chat_Client::handle_connection, this, boost::asio::placeholders::error);

谁能给我解释一下,这个符号 &Chat_Client::handle_connection 是什么意思?我以前没见过这样的语法。 handle_connection 函数不是 static。文档说

bind(&X::f, args) is equivalent to bind(mem_fn(&X::f), args)

情况并没有变得更清楚。紧急求救

绑定采用函数指针或函子。

handle_connection 是一个函数。具体来说,就是Chat_Client的一个成员函数。一个函数,从内存中可执行部分的某处开始。要调用一个函数,您需要在该地址开始执行代码。将函数绑定到 boost::function 变量时,记住的是函数指针(当我尝试调用此 boost::function 时我应该去哪里?)。

handle_connectionChat_Client中实现。在内存中,该函数将被布局一次,并使用指向当前实例的不同 this 指针简单地调用。这就是为什么:

  1. 您仍然使用&Chat_Client::handle_connection
  2. 指定函数
  3. 您需要将 this 作为第一个绑定参数传递,即使 Chat_Client* 不是 handle_connection 签名的一部分。