The Conversion between CString and LPCTSTR

 
         CString str = _T("Test");
         LPCTSTR p = (LPCTSTR)str;// LPCTSTR means: long pointer of const TChar string. LPTSTR
         CString strAnother1(p);
         CString strAnother2 = p;
 
Actually, I guess that there is an operator override for CString. For example, I write a brand new string class.
 
class SimonStr
{
public:
         SimonStr(TCHAR* pChar);
         TCHAR* GetRawPointer() const ;
 
         operator TCHAR* ()
        {
                 return GetRawPointer();
        }
        
private:
         TCHAR* m_pChar;
         size_t m_length;
};
 
SimonStr::SimonStr(TCHAR* pChar)
{
         m_pChar = pChar;
         m_length = _tcslen(pChar);/*strlenT(pChar); It's used for Unicode char*/
}
 
TCHAR* SimonStr::GetRawPointer() const
{
         return m_pChar;
}
 
Then we can do the conversion below:
SimonStr simonStr(_T("Test2"));
LPCTSTR p2 = (LPCTSTR)simonStr;

你可能感兴趣的:(职场,休闲)