为什么LPTSTR GetPath();函数不能返回CString定义的变量?但是LPCTSTR GetPath();就可以?如果我没记错的话,LPCTSTR只是比LPTSTR多了一个限定词const,请教了~~~~~~~~~~~~~~~~~~~~~~~~~~

解决方案 »

  1.   

    CString有一个转换到LPCTSTR的运算符重载。 
      

  2.   

    CString str;
    str.GetBuffer(0);//LPTSTR
      

  3.   

    LZ的意思是:
    LPTSTR GetPath()
    {
        CString ret;
        //代码
        return ret;
    }
    这怎么行呢?返回临时对象没到LPTSTR的隐式转换。
    能转换意味着返回值能被修改,但返回值都析构了,怎么修改。
      

  4.   

    PXSTR GetBuffer( int nMinBufferLength )
    {
    return( PrepareWrite( nMinBufferLength ) );
    }
    /////////////////////////////////////////////////////////////////
    PXSTR PrepareWrite( int nLength )
    {
    CStringData* pOldData = GetData();
    int nShared = 1-pOldData->nRefs;  // nShared < 0 means true, >= 0 means false
    int nTooShort = pOldData->nAllocLength-nLength;  // nTooShort < 0 means true, >= 0 means false
    if( (nShared|nTooShort) < 0 )  // If either sign bit is set (i.e. either is less than zero), we need to copy data
    {
    PrepareWrite2( nLength );
    } return( m_pszData );
    }
    //////////////////////////////////////////////////////////////////
    ATL_NOINLINE void PrepareWrite2( int nLength )
    {
    CStringData* pOldData = GetData();
    if( pOldData->nDataLength > nLength )
    {
    nLength = pOldData->nDataLength;
    }
    if( pOldData->IsShared() )
    {
    Fork( nLength );
    }
    else if( pOldData->nAllocLength < nLength )
    {
    // Grow exponentially, until we hit 1K.
    int nNewLength = pOldData->nAllocLength;
    if( nNewLength > 1024 )
    {
    nNewLength += 1024;
    }
    else
    {
    nNewLength *= 2;
    }
    if( nNewLength < nLength )
    {
    nNewLength = nLength;
    }
    Reallocate( nNewLength );
    }
    }知道了吧 写atl的又不是傻帽 咋会返回临时指针呢
      

  5.   


    是不是应该返回str.GetBuffer(MAX_PATH) ?
      

  6.   

    我很奇怪,为什么很多人提到 GetBuffer, 却没有人提到 GetString() ?CString str;
    TCHAR pbuf = str.GetString();....