控件 在类之间的 传递

三种方法,拿项目中的代码来说明:

一:引用传递

二:指针传递

三:获得父窗口

对于三就不多说了,对于一二,看代码便知,引用:

 CGroupAdvance* dlg = new CGroupAdvance(m_OtherListCtr);
 dlg -> DoModal();
 delete dlg;

指针:

 CGroupAdvance* dlg = new CGroupAdvance(&m_OtherListCtr);
 dlg -> DoModal();
 delete dlg;

代码只有有或无&的区别,一个是指针,一个是引用,对应的构造函数也应该做相应变化,对于引用:

CListCtrl&  m_OtherList;//定义成引用,不能定义为CListCtrl  m_OtherList

CGroupAdvance::CGroupAdvance(CListCtrl& tmpListCtr,CWnd* pParent /*=NULL*/)
 : CDialog(CGroupAdvance::IDD, pParent)
 ,m_OtherList(tmpListCtr)//必须在这里初始化
{
}

对于指针:

CListCtrl*  m_pOtherList;

CGroupAdvance::CGroupAdvance(CListCtrl* tmpListCtr,CWnd* pParent /*=NULL*/)
 : CDialog(CGroupAdvance::IDD, pParent)
{

m_pOtherList  =  tmpListCtr;

}

 优缺点:引用不用判断可用,指针需要判断

原文地址:https://www.cnblogs.com/8586/p/1422541.html