C++ 中静态成员函数访问非静态成员变量的方法
最近在 VS2010 里开发出厂烧写工具,遇到一个问题:
我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的。最后找到的办法是用对象指针来做。
sourcecode:
1 #test.h 2 3 class Test 4 { 5 protect: 6 static UINT threadFun(LPVOID pParam); 7 8 private: 9 int a; 10 }
1 #test.cpp 2 3 4 AfxBeginThread(CTestThreadApplyDlg::threadFun, this); 5 6 UINT CTestThreadApplyDlg::threadFun(LPVOID pParam) 7 { 8 CTestThreadApplyDlg *test = (CTestThreadApplyDlg*)pParam; 9 test->a = 8; 10 11 afxDump << "a" << test->a << "\n"; 12 return 0; 13 }
一点要注意的是 threadFun 的返回值必须是 UINT ,否则会提示错误。
当你坚持做一件完全正确的事情,有可能在很长一段时间内,你的价值都是零。
posted on 2015-12-24 02:34 Jeffrey.Downhouse 阅读(535) 评论(0) 编辑 收藏 举报