简单 但 容易出错的地方

来源:互联网 发布:mac brick o la试色 编辑:程序博客网 时间:2024/06/05 20:00

持续跟新中

1. 当使用容器定义指针类型的时候 要注意这个指针指向内容的变化

std::vector<char *> m_vPath;char str[20] = "hello world"; int main(){for (int i = 0; i < 2; i++){m_vPath.push_back(str);strcpy(str,"123455");}}
上面本意是想将字符串"hello world" 和 "123455"存入容器中 ,但是最后的结果是是"123455","123455"  ,指针指向的内容已经发生变化




2.多线程问题:使用了公用 变量经常容易出错



在类中声明了一个类对象 m_ftp,,但是在线程函数DownLoad中对该对象进行了操作, 这样导致在主线程中如果在使用到的该对象的时候可能发生改变,使用的时候要考虑该值的变化对程序可有影响

void CFtpDownLoadDlg::DownLoadFile(CString LocalFoldPath,CString ServerFilePath,CString ServerFileName){CString str;str = LocalFoldPath;if ( str.Right( 1 ) != "\\" )str += "\\" + ServerFileName;elsestr += ServerFileName;m_nCurUpFileSize = 0;//计算远程文件大小m_ftp.Size(( LPSTR )( LPCTSTR )ServerFilePath, &m_nCurUpFileSize, ftplib::image);//设置下载信息回调m_ftp.SetCallbackXferFunction( ( FtpCallbackXfer )CallbackXfer, this, 10240 );int a = m_ftp.Get(( LPSTR )( LPCTSTR )str, ( LPSTR )( LPCTSTR )ServerFilePath, ftplib::image );if ( a > 0 ){  //BeginFindLocFile();}else{//MessageBox( _T( "文件下载失败,请检查是否登录!" ), _T( "提示" ), MB_ICONWARNING );}}





0 0
原创粉丝点击