Using MFC to serialize data and C + + objects

Source: Internet
Author: User

Serialization of data

--Example program: Memo

Create a new single document SDI application, the view class selects CFormView so that users can enter them in the window. Create three edit boxes in the interface, and then add three corresponding edit box variables. These three variables are member variables of the view class, and in order to interact with the data, three corresponding variables are also created in the document class. Both the document class and the view class then initialize the data member, which is typically done in the onnewdocument () function in the document class. The document class OnNewDocument () function executes when any of the following actions occur:

When the user launches the application;

When the user selects the "New" option from the "File" menu;

The initialization of a view class is usually held by OnInitialUpdate (), and the code triggers the View class OnInitialUpdate () function execution when any of the following actions occur:

When the user launches the application;

When the user selects the "New" option from the "File" menu;

When the user selects the "Open" option from the "File" menu;

The way to get a document class pointer in a view class is: cfoodoc* PDoc = Gerdocument ();

Use this document pointer to manipulate document class data: M_viewdata = pdoc->m_docdata;

The serialized code is simple, and AR is a Document object (CArchive object) corresponding to the file selected by the user:

// CFooDoc 序列化
void CFooDoc::Serialize(CArchive& ar)
{

if (ar.IsStoring())

{

// 将数据写入文件

 ar << m_DocData;

}

else

{

// 从文件中读取数据

 ar >> m_DocData;

}
}

This writes the data to the file, selecting Save or Save as in the "File" menu to serialize the data. If you do not save the data, the exit program prompts the user to save the modified data. Please refer to the source code for specific details.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.