C++ std::bind思考
2016-05-20 16:01
351 查看
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是怎么做到还能成功调用对象的成员函数的呢?这点不太懂
相关文章推荐
- <<Effective C++>>读书笔记1: 让自己习惯C++
- C++著名类库
- L1-010. 比较大小(C++)
- 简化版 栈的C++实现及栈的函数模板
- c++学习之运算符重载
- PAT (Basic Level) Practise (中文)1052. 卖个萌 (20)
- 关于C++ vector<S>使用时,错误 5 error C2719: “_Val”: 具有 __declspec(align('16')) 的形参将不被对齐
- 计蒜客之整除问题
- win32程序使用C++/CX语法
- C++ inline函数和template函数
- c++ primer plus 第13章习题
- L1-007. 念数字(C++)
- C++ Primer Plus第6版18个重点笔记
- c++primer plus 第12章习题
- C++sort()函数的用法
- java面试需要准备什么呢?c++可以看看
- c++类型转换运算符
- C++ builder 添加资源文件
- 【C++】内联inline、继承、重载与虚函数的解释
- c++调用python