

//反串行化
CFile fileLoad(_T("串行化檔案.txt"), CFile::modeRead);
CArchive arLoad(&fileLoad, CArchive::load, 1024, buf);
int nLoad = 0;
CString strResult;
arLoad >> nLoad;
CBook* pBooksLoad = new CBook;
for (int i = 0; i<nCount; ++i)
{
arLoad >> pBooksLoad;
}
strResult.Format(_T("%s"),pBooksLoad);
AfxMessageBox(strResult);
arLoad.Close();
fileLoad.Close();
return 0;
問題出現在:
strResult.Format(_T("%s"),pBooksLoad);
AfxMessageBox(strResult);怎樣才能讓strResult里的字串是*pBooksLoad里面的內容呢?
uj5u.com熱心網友回復:
你得寫型別轉換運算子函式啊,或者寫一個單獨的函式
class CBook
{
public:
//轉換成CString
operator const CString&() { return m_strName; }
//轉換成字串指標
operator const PTCHAR() { return m_strName.GetBuffer(); }
//單獨的函式
const CString& GetName() const { return m_strName; }
};
//使用型別轉換
strResult.Format(_T("%s"),*pBooksLoad);
//使用函式
strResult.Format(_T("%s"),pBooksLoad->GetName());
uj5u.com熱心網友回復:
strResult.Format(_T("%s"),pBooksLoad);要保證此句代碼正確運行,pBooksLoad必須是一個字串,而不是一個CBook*
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/136592.html
標籤:基礎類
上一篇:MFC 值傳遞問題
