有一個類
class CMiRegularPath
{
public:
CMiRegularPath(LPCTSTR lpPath);
~CMiRegularPath();
operator LPCTSTR ();
operator LPTSTR();
protected:
LPTSTR m_lpPathBuffer;
LPCTSTR m_lpPath;
LPCTSTR m_lpPathRegular;
};
下面是調用
void func1(LPTSTR lpPointer)
{
}
void func2(LPCTSTR lpPointer)
{
}
void test
{
func1(CMiRegularPath(_T("D:\\aa/AS")));
func2(CMiRegularPath(_T("D:\\aa/AS")));
LPTSTR lpP1 = CMiRegularPath(_T("D:\\aa/AS"));
LPCTSTR lpP2 = CMiRegularPath(_T("D:\\aa/AS"));
}
真相
test函數中前兩個調用是對的,在func1和func2中能得到正確的字符串,後兩個是錯誤的,執行完這一行後馬上就會執行類的析構函數,所以lpP1,lpP2不會指向真正的字符串。