Home > Software engineering >  MFC point problems the serialization display pointer content?
MFC point problems the serialization display pointer content?

Time:10-23



//the serialization 
CFile fileLoad (_T (" serialization files. TXT "), CFile: : modeRead);
CArchive arLoad (& amp; FileLoad CArchive: : load, 1024, buf);
Int nLoad=0;
Cstrings strResult;
ArLoad & gt;> NLoad;
CBook * pBooksLoad=new CBook;
For (int I=0; i{
ArLoad & gt;> PBooksLoad;
}
StrResult. The Format (_T (" % s "), pBooksLoad);
AfxMessageBox (strResult);
ArLoad. Close ();
FileLoad. Close ();

return 0;


Problems in:
 strResult. The Format (_T (" % s "), pBooksLoad); 
AfxMessageBox (strResult);

How to make the string in the strResult is * pBooksLoad the content inside?

CodePudding user response:

You have to write a type conversion operator function, or write a separate function
 
The class CBook
{
Public:
//convert cstrings
Operator const CString& () {return m_strName; }
//converted to string pointer
Operator const PTCHAR () {return m_strName. GetBuffer (); }
//separate function
Const CString& Const GetName () {return m_strName; }
};

//use the type conversion
StrResult. The Format (_T (" % s "), * pBooksLoad);
//using the function
StrResult. The Format (_T (" % s "), pBooksLoad - & gt; GetName ());

CodePudding user response:

StrResult. The Format (_T (" % s "), pBooksLoad);

To ensure that the correct operation code, pBooksLoad must be a string, rather than a CBook *
  • Related