0
回答
强大的CArchive,别告诉我你只会用它处理文本
终于搞明白,存储TCO原来是这样算的>>>   

     不想多说,程序里已经写明。请各位同学务必看仔细其中差异。:)

 

#include <afx.h>
#include <iostream>


class CTest : public CObject
{
public:
	CTest():m_a(100){Set();}

public:
	void Set()
	{
	   m_a = 888;
	} 
	void Set2()
	{
	  m_a = 777;
	}
	void Get()const
	{
		std::cout<<m_a<<std::endl;
	}

public:
	virtual void Serialize(CArchive& ar){}
private:
	int m_a;

protected :
	DECLARE_SERIAL(CTest)
};

class CTest2 : public CObject
{
public:
	CTest*   m_obj;

public:
	CTest2()
	{
	   m_obj = new CTest;
	}
	~CTest2()
	{
	   Release();
	}
	void Release()
	{
	 if(m_obj)
	   {
	     delete m_obj;
		 m_obj = NULL;
	   }
	}
	virtual void Serialize(CArchive& ar);

protected:
	DECLARE_SERIAL(CTest2)
};

IMPLEMENT_SERIAL(CTest, CObject, 23)
IMPLEMENT_SERIAL(CTest2, CObject, 23)

void CTest2 :: Serialize(CArchive& ar)
{
	CObject::Serialize(ar);

	if(ar.IsStoring())
	{
	   m_obj->Set2();// pay attention to here
	   ar<< m_obj;
	}
	else
	{
	   Release();
	   ar>>m_obj;
	   m_obj->Get();
	}

	return;
}

int main()
{
	CTest2 obj;

    CFile iof("template.cplus", CFile::modeWrite | CFile::modeCreate );
         CArchive arch(&iof, CArchive::store);
	obj.Serialize(arch);
	arch.Close();
    iof.Close();

    CFile oof("template.cplus", CFile::modeRead );
	     CArchive arch2(&oof, CArchive::load);
    obj.Serialize(arch2);

	arch2.Close();
	oof.Close();

    return 0;
}


 


原文链接:http://blog.csdn.net/template_cplus/article/details/7888067
MFC
举报
晨曦之光
发帖于5年前 0回/192阅
顶部