strPath = CGlobal::m_BackupAddress;
strPath += strTime;
strPath = "C:\\Backup\\";
strPath += strTime;如上两段代码,第一段中CGlobal::m_BackupAddress也是CString型的,我单步到strPath = CGlobal::m_BackupAddress时 strPath在监视窗里面看到的内容是"C:\Backup",继续单步一步,加上strTime之后,strPath居然等于strTime了!第二段代码能够正常运行!!!
尝试了几次都不行,而且第一段中我还把strPath赋值给一个strTemp,然后让strTemp+= strTime,居然strTemp也等于strTime了!太诡异了!

解决方案 »

  1.   

    我在我第一段代码
     strPath = CGlobal::m_BackupAddress;之后用了AfxMesssageBox(strPath)输出C:\Backup\"
     strPath += strTime; AfxMessageBox(strPath) 输出"2012-8-28"
      

  2.   

    而且用lstrcat得到的新的strPath 如果再直接用"+"去连接别的字符串,结果strPath还是加不进去!!!!!
      

  3.   

    strPath在监视窗里面看到的内容是"C:\Backup"不是应该是strPath在监视窗里面看到的内容是"C:\Backup\"
      

  4.   


    CGloabl
    {
    public:
    static CString m_BackupAddress;
    }这是我m_BackupAddress的定义,不会有问题吧?
      

  5.   

    监视窗里面是"C:\Backup\",我刚刚打错了!
      

  6.   

    要是我一般就这样写
    strPath.Format(_T("%s%s"), CGlobal::m_BackupAddress, strTime);
    AfxMessageBox(strPath);
      

  7.   

    这样的确可行! 就是为什么直接"+"不行? 因为是static的吗?
      

  8.   

    static 的意思是声明局部静态变量,静态变量和普通变量的存储位置不同,你将一个
    静态变量赋值给一个普通变量只是暂时改变了普通变量的值,在下一次赋值时,因为存储
    位置不同所以值被重新覆盖了。
      

  9.   


    class CTest
    {
    public:
    static int a;
    };
    int CTest::a = 1;
    void main()
    {
    int b(0),c(1);
    b = CTest::a;
    b += c;
    cout<<b<<endl;
    }
    按照你这个说法的话,这段代码是不是应该输出1? 但是我看到输出的是2!!!
      

  10.   

    CGloabl
    {
    public:
        static CString m_BackupAddress;
    }
    //
    m_BackupAddress;在那里赋值?
      

  11.   

        我大概试出来在哪出问题了,我在程序初始化的时候用::GetPrivateProfileString函数从我的ini文件里面读出来的值! 我猜问题应该是在这里,具体怎么解决我还不知道!
        我发现用::GetPrivateProfileString读出来的值,貌似不能进行一部分操作! 不知道是不是!!
      

  12.   

    DWORD GetPrivateProfileString(
      LPCTSTR lpAppName,        // points to section name
      LPCTSTR lpKeyName,        // points to key name
      LPCTSTR lpDefault,        // points to default string
      LPTSTR lpReturnedString,  // points to destination buffer
      DWORD nSize,              // size of destination buffer
      LPCTSTR lpFileName        // points to initialization filename
    );
     
    File: "C:\Program Files\Microsoft Visual Studio\VC98\Include\WINNT.H" 2 occurrences found on 2 lines
       182: typedef LPCWSTR LPCTSTR;
       196: typedef LPCSTR LPCTSTR;
    File: "C:\Program Files\Microsoft Visual Studio\VC98\Include\WTYPES.H" 1 occurrences found on 1 lines
       209: typedef /* [string] */ const CHAR __RPC_FAR *LPCSTR;
      

  13.   

    原来是const的问题,我知道const的值是不可修该的.那const的值还是不可进行运算的吗? ::GetPrivateProfileString("Backup","time","",strTemp.GetBuffer(50),50,".//test.ini");
    CString s1,s2;
    s1 = "111";
    s2 =strTemp+s1;这段代码最后为什么s2还是"111"?
      

  14.   


    个人估计是你用GetPrivateProfileString函数用的有问题,导致栈上的数据出错...
    例如GetPrivateProfileString("Student","Name","DefaultName",SName.GetBuffer(MAX_LENGTH),MAX_LENGTH,"c:\\setting.ini");结果:SName = "jacky";这里需要注意点就是用完GetBuffer函数后一定要释放(用SName.ReleaseBuffer()函数),不然后面再用到SName的其他子函数就会失灵。
      

  15.   

    注意:在这里使用CString变量时,在使用完GetBuffer后,紧接着一定要使用ReleaseBuffer()函数,才可以进行其他的诸如字符串+操作
      

  16.   

    记住 strTemp.GetBuffer一定要Release
      

  17.   

    就是这样,谢谢怪龙兄了! 做MFC不久,经常不知道要release!