C++ std::bind思考

来源:互联网 发布:炉石传说mac版2017 编辑:程序博客网 时间:2024/05/16 18:19
class test_rcp{public:test_rcp();~test_rcp();test_rcp(const test_rcp& c){m_i = c.m_i;}public:void testrpc_1(){cout << "in testrpc_1\t" << m_i<<endl;}public:int m_i;};test_rcp::test_rcp(){m_i = 200;}test_rcp::~test_rcp(){}int main(){test_rcp* testrpc = new test_rcp; //auto f = std::bind(&test_rcp::testrpc_1, *testrpc);auto f = std::bind(&test_rcp::testrpc_1, testrpc);testrpc->m_i+=1;//delete testrpc;f();cin.get();return 0;}


1.当std::bind绑定的是对象,会触发 test_rcp(const test_rcp& c) 拷贝构造函数,且输出的结果为:in testrpc_1 200
说明存入对象std::bind会对对象就行拷贝
2.当std::bind绑定的是对象的指针,并不会触发 test_rcp(const test_rcp& c) 拷贝构造函数,且输出的结果:in testrpc_1 201
说明绑定对象指针时,只是单纯的复制对象指针
3.在调用std::bind绑定函数之前delete testrpc,传递指针的情况下m_i的值不规律,因为该指针指向的地址已经被释放了,该调用引用了非法的内存地址
4.在对象已经被析构的情况下std::bind是怎么做到还能成功调用对象的成员函数的呢?这点不太懂

0 0
原创粉丝点击