关于Doc类和View类中对象的构造和析构问题
请教:我在View类中引用Doc类中的对象时发现Doc类中的对象被构造一次,却出现了两次析构过程!程序代码如下:
void CHuffmanView::OnCoder()
{
// TODO: Add your command handler code here
//输入一个文件对之编码
CCoderDlg dlg;
ifstream in;
ofstream out;
CHuffmanDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(IDOK==dlg.DoModal())
{
while(!dlg.m_selected)
{
MessageBox("请选择一种编码方式!","错误!");
if(IDCANCEL==dlg.DoModal())
break;
}
if(dlg.m_creatNow) //现在创建编码
{
OnpreProcess(); //预处理,构建一个Huffman树
in.open(dlg.m_filePath_S);
if(IDYES==MessageBox("是否输出到密码文件中?","请选择",MB_YESNO))
out.open(dlg.m_filePath_D);
SetWindowText(pDoc->getTree().HCoder(in,out));
in.close();
if(out)
out.close();
}
if(dlg.m_justCreated) //已经创建了编码
{
if(pDoc->getTree().getRoot()==NULL)
MessageBox("你还没有创建编码表","操作失败!");
else
{
in.open(dlg.m_filePath_S);
if(IDYES==MessageBox("是否输出到密码文件中?","请选择",MB_YESNO))
out.open(dlg.m_filePath_D);
SetWindowText(pDoc->getTree().HCoder(in,out));
in.close();
if(out)
out.close();
}
}
if(dlg.m_useExisted) //使用已有的Huffman树
{
pDoc->setTag();
CFile file;
file.Open(dlg.m_loadPath,CFile::modeRead);
CArchive arch(&file,CArchive::load);
pDoc->Serialize(arch); //读取编码
arch.Close();
file.Close();
in.open(dlg.m_filePath_S); //编码操作
if(IDYES==MessageBox("是否输出到密码文件中?","请选择",MB_YESNO))
out.open(dlg.m_filePath_D);
SetWindowText(pDoc->getTree().HCoder(in,out));
in.close();
if(out)
out.close();
}
}
}
上面这段代码中,pDoc->getTree()返回一个CTree类指针(我定义的类),CTree是Doc类的一个私有成员,根据我的理解,CTree应该在Doc类析构之前才析构CTree的对象,可是上为什么在上面这段程序中,执行SetWindowText(pDoc->getTree().HCoder(in,out))之后就执行了一次CTree对象的析构过程,然后在关闭窗口时,又执行了一次CTree对象的析构,我不明白的是为什么会出现第一次析构过程?(我用单步方式调试时,发现CTree对象值构造了一次)