boost::bind(&memberfunction, obj, _1, _2........)类似这样的用法,我们叫做成员函数绑定,boost库的文档中说的很清楚,第一个参数可以是value、pointer和reference,即传值、传地址和传引用都是可以的,所以在一般情况下,下面三种使用bind的形式都是成立的。

 class A
class A


 {
{
 public:
public:
 void func();
    void func();
 };
};


 A a;
A a;
 A& r = a;
A& r = a;

 boost::bind(&A::func, a);
boost::bind(&A::func, a);
 boost::bind(&a::func, &a);
boost::bind(&a::func, &a);
 boost::bind(&a::func, r);
boost::bind(&a::func, r);由上面的代码可以看出,我们可以随便传任意一种类对象的形式,函数模板会自动寻找最为匹配的为我们实现。但是有两种情况是特殊的,即:
1、该对象不可进行拷贝构造函数。
2、该对象不可随意被析构。
发现这个问题是在我编写单件模式时的遇见的,当时发现我的单件对象在bind中被析构了一次,这很不寻常,为什么bind会调用第一个参数的析构呢?跟踪进了boost的源码才发现,原来所有的参数都会被拷贝一遍,然后析构一遍,这样一来,我们传递参数的时候就会有一些小麻烦了,首先必须保证参数能够被拷贝而不影响逻辑和数据一致性,其次,参数能够被析构而不影响逻辑和数据一致性。单件是全局性质的数据,所以绝对不可以析构,那么这种情况的话,我们只好传递单件对象的地址,而不能传递值或引用。
另:附上出错问题的代码如下
 class InputDevice
class InputDevice
 : public EventSource
    : public EventSource
 , public Singleton<InputDevice>
    , public Singleton<InputDevice>


 {
{
 public:
public:
 
    
 };
};

 class TestUI
class TestUI
 : public Singleton<TestUI>
    : public Singleton<TestUI>


 {
{
 public:
public:

 ~TestUI()
    ~TestUI() {
{
 std::cout<<"~TestUI"<<std::endl;
        std::cout<<"~TestUI"<<std::endl;
 }
    }

 void processKeyboard(EventArgs& args)
    void processKeyboard(EventArgs& args) {
{
 std::cout<<"键盘响应"<<std::endl;
        std::cout<<"键盘响应"<<std::endl;
 }
    }


 void processMouse(EventArgs& args)
    void processMouse(EventArgs& args) {
{
 std::cout<<"鼠标响应"<<std::endl;
        std::cout<<"鼠标响应"<<std::endl;
 }
    }
 };
};


 int _tmain(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[])


 {
{
 new FrameUpdaterManager;
    new FrameUpdaterManager;
 new DelayEventSender;
    new DelayEventSender;
 new InputDevice;
    new InputDevice;
 new TestUI;
    new TestUI;

 InputDevice::getSingleton().mEventSet.addEvent("KeyDown", Event());
    InputDevice::getSingleton().mEventSet.addEvent("KeyDown", Event());
 InputDevice::getSingleton().mEventSet.addEvent("KeyUp", Event());
    InputDevice::getSingleton().mEventSet.addEvent("KeyUp", Event());
 InputDevice::getSingleton().mEventSet.addEvent("MouseLDown", Event());
    InputDevice::getSingleton().mEventSet.addEvent("MouseLDown", Event());
 InputDevice::getSingleton().mEventSet.addEvent("MouseLUp", Event());
    InputDevice::getSingleton().mEventSet.addEvent("MouseLUp", Event());
 InputDevice::getSingleton().mEventSet.addEvent("MouseRDown", Event());
    InputDevice::getSingleton().mEventSet.addEvent("MouseRDown", Event());
 InputDevice::getSingleton().mEventSet.addEvent("MouseRUp", Event());
    InputDevice::getSingleton().mEventSet.addEvent("MouseRUp", Event());


 //TestUI& ui = TestUI::getSingleton(); // 用此行便会出错
    //TestUI& ui = TestUI::getSingleton(); // 用此行便会出错
 TestUI* ui = TestUI::getSingletonPtr();
    TestUI* ui = TestUI::getSingletonPtr();

 // 出错开始
    // 出错开始
 InputDevice::getSingleton().mEventSet["KeyDown"] += boost::bind(&TestUI::processKeyboard, ui, _1);
    InputDevice::getSingleton().mEventSet["KeyDown"] += boost::bind(&TestUI::processKeyboard, ui, _1);
 InputDevice::getSingleton().mEventSet["KeyUp"] += boost::bind(&TestUI::processKeyboard, ui, _1);
    InputDevice::getSingleton().mEventSet["KeyUp"] += boost::bind(&TestUI::processKeyboard, ui, _1);

 InputDevice::getSingleton().mEventSet["MouseLDown"] += boost::bind(&TestUI::processMouse, ui, _1);
    InputDevice::getSingleton().mEventSet["MouseLDown"] += boost::bind(&TestUI::processMouse, ui, _1);
 InputDevice::getSingleton().mEventSet["MouseLUp"] += boost::bind(&TestUI::processMouse, ui, _1);
    InputDevice::getSingleton().mEventSet["MouseLUp"] += boost::bind(&TestUI::processMouse, ui, _1);
 InputDevice::getSingleton().mEventSet["MouseRDown"] += boost::bind(&TestUI::processMouse, ui, _1);
    InputDevice::getSingleton().mEventSet["MouseRDown"] += boost::bind(&TestUI::processMouse, ui, _1);
 InputDevice::getSingleton().mEventSet["MouseRUp"] += boost::bind(&TestUI::processMouse, ui, _1);
    InputDevice::getSingleton().mEventSet["MouseRUp"] += boost::bind(&TestUI::processMouse, ui, _1);


 delete TestUI::getSingletonPtr();
    delete TestUI::getSingletonPtr();
 delete InputDevice::getSingletonPtr();
    delete InputDevice::getSingletonPtr();
 delete DelayEventSender::getSingletonPtr();
    delete DelayEventSender::getSingletonPtr();
 delete FrameUpdaterManager::getSingletonPtr();
    delete FrameUpdaterManager::getSingletonPtr();
 return 0;
    return 0;
 }
}
