MFC CArchive销毁对象 [英] MFC CArchive destroy object

查看:82
本文介绍了MFC CArchive销毁对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2008 MFC项目.我有以下代码:

I am using VS2008 MFC project. I have the following code:

TCHAR* pszFileName = _T("c:\\ProjectOne Code\\ProjectOne\\ProjectOne\\test.txt");
CFile f;
f.Open(pszFileName, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&f, CArchive::store);
for(x=0; x<7; x++)
{
    numarray[x][0] = m_ListCtrl.GetItemText(x,0);
    ar << numarray[x][0];
    numarray[x][1] = m_ListCtrl.GetItemText(x,1);
    ar << numarray[x][1];
}
ar.Close();
f.Close();


该代码按预期将所有信息写入我的文本文件.然后,我想重新读回文件内容,以确认数据已正确存储.

如果尝试在同一函数中执行此操作:CArchive ar(&amp;f, CArchive::load);,则会收到一条错误消息,指出我正在尝试重新定义ar.有没有一种方法可以删除ar对象,以便重新创建它以进行加载?我知道如果离开该功能,该对象将自动销毁.但是应该有一种在函数中销毁它的方法,以便可以重复使用相同的对象名称.

如果我在函数中使用以下代码:


The code writes all the information to my text file as expected. I then want to read the file contents back in to confirm data is stored correctly.

If I the try to do this: CArchive ar(&amp;f, CArchive::load); in the same function, I get an error stating I am trying to redefine ar. Is there a way to delete the ar object so I can recreate it for loading? I know if I leave the function the object is destroyed automatically. But there should be a way to destroy it in the function so the same object name can be reused.

If I use the following code in the function:

CArchive br(&f, CArchive::load);
f.Open(pszFileName, CFile::modeRead);
for(x=0; x<7; x++)
{
    numarray[x][0] = "";
    numarray[x][1] = "";
}
for(x=0; x<7; x++)
{
    br >> numarray[x][0];
    br >> numarray[x][1];
    MessageBox(numarray[x][0], numarray[x][1]);
}
br.Close();
f.Close();


我能够从文件中读取信息并确认它是正确的.使用其他对象并没有真正的问题,但是我很好奇为什么我不能删除原始ar对象并重新使用它.


I am able to read back the information from the file and confirm it is correct. There is no real issue with using a different object but I am just curious as to why I can''t delete the original ar object and re-use it.

推荐答案

在第一个函数中,归档对象ar在函数中实例化,因此仅当它超出范围时(即,当您退出函数时)才会被销毁.如您所见,您无法将同一对象重新声明为不同于其第一个声明的对象.如果您想重用它,则可以使ar成为以下指针:CArchive* ar,并使用newdelete对其进行管理.
In your first function your archive object ar is instantiated within the function, so it will only be destroyed when it goes out of scope, i.e. when you exit the function. As you have seem you cannot redeclare the same object to be something different from its first declaration. If you wish to reuse it then make ar a pointer thus: CArchive* ar, and use new and delete to manage it.


2buck56写道:
2buck56 wrote:

理查德,谢谢你的建议.但是,我尝试将CArchive * ar与new和delete一起使用.即使删除了ar指针,也无法重新使用它.编译器抱怨相同的错误消息.如果仍然要创建2个对象,则最好使用ar进行写入,使用br进行读取. -

Richard, thanks for the suggestion. However, I''ve tried using CArchive* ar with new and delete. Even after you delete the ar pointer, you cannot re-use it. The compiler complains with the same error message. If I am going to have to create 2 objects anyway, I might as well use ar for writing and br for reading. -



如果在删除后重用对象,编译器将不会抱怨,也许您正试图以这种方式重新声明它:



The compiler will not complain if you reuse an object following a delete, perhaps you are trying to redeclare it thus:

CArchive* ar = new Carchive(&f, CArchive::store);
...
delete ar;
CArchive* ar = new Carchive(&f, CArchive::load);
...


而您的第二条语句应写为


whereas your second statement should be written as

ar = new Carchive(&f, CArchive::load);


您已经声明了ar作为指向CArchive对象的指针,因此您不必重新声明它.


You have already declared ar as a pointer to a CArchive object so you must not redeclare it.


您还可以重用一个函数:):
You could reuse a function as well :) :
class CYourArray
{
...
  static LPCTSTR sm_lpszDefFileName;
...
public:
...
  bool ExchangeData(bool bLoad, LPCTSTR lpszFileName = sm_lpszDefFileName);
};


这篇关于MFC CArchive销毁对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆