英文:
Can I prevent the need to pass this into inherited function as a parameter?
问题
基类1:
class CBaseEditor
{
public:
bool CBaseEditor::ImportTemplate(CWnd* pParent)
{
CFileDialog dlgImport(TRUE,
_T(".XSL"), _T(""),
OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
_T("FilterText"), pParent);
}
};
基类2:
class CResizingDialog : public CDialogEx
{
};
从这两个基类派生的对话框类:
class CFieldServiceGroupReportDlg : public CResizingDialog, public CBaseEditor
{
private:
void SuneFunc()
{
ImportTemplate(this);
}
};
我的ImportTemplate
函数,有没有办法在不传递this
的情况下让它访问调用者的this
?
根据提供的评论,我得出的结论是最好的做法是更改我的现有构造函数(字段类已被重命名):
CPublishersDatabaseViewerDlg::CPublishersDatabaseViewerDlg(CWnd* pParent /*=nullptr*/)
: CResizingDialog(L"GroupsReport", IDD_DIALOG_PUBLISHERS_DATABASE_VIEWER, pParent)
, m_strPreviewXSL(L"")
{
m_strPreviewXSL = theApp.GetStringSetting(L"Options", L"PublisherDB_Style", L"PublisherDB--Field Service Groups");
}
按设计,对话框类接受一个父窗口。同样,我应该以某种方式更改CBaseEditor
以接受适当的父窗口。然后我就不需要每次都传递它了。这是我的基类构造函数:
CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent /* nullptr */, bool bOnlyStorePosition /* false */)
: CDialogEx(nIDTemplate, pParent)
, m_bDoNotShowResizeIcon(false)
, m_bOnlyStorePosition(bOnlyStorePosition)
, m_bLimitToHorizontalResizing(false)
, m_strWindowID(strWindowID)
{
m_rcInit.SetRect(0, 0, 0, 0);
}
CBaseEditor::CBaseEditor() noexcept(false)
{
Init();
}
英文:
Base class 1:
class CBaseEditor
{
public:
bool CBaseEditor::ImportTemplate(CWnd* pParent)
{
CFileDialog dlgImport(TRUE,
_T(".XSL"), _T(""),
OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
_T("FilterText"), pParent);
}
}
Base class 2:
class CResizingDialog : public CDialogEx
{
}
Dialog class, derived from both bases:
class CFieldServiceGroupReportDlg : public CResizingDialog, public CBaseEditor
{
private:
void SuneFunc()
{
ImportTemplate(this);
}
}
My ImportTemplate
function, is there anyway for it to get access to the "this" from the caller without having to pass "this" in?
Based on the comment provided I have come to the conclusion that the best thing to do is change my existing constructor (field class has since been renamed):
CPublishersDatabaseViewerDlg::CPublishersDatabaseViewerDlg(CWnd* pParent /*=nullptr*/)
: CResizingDialog(L"GroupsReport", IDD_DIALOG_PUBLISHERS_DATABASE_VIEWER, pParent)
, m_strPreviewXSL(L"")
{
m_strPreviewXSL = theApp.GetStringSetting(L"Options", L"PublisherDB_Style", L"PublisherDB--Field Service Groups");
}
By design the dialog class accepts a parent. Likewise, I should change CBaseEditor
to accept an appropriate parent in some way. Than I won't need to pass it all the time. This is my base constructors:
CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent /* nullptr */, bool bOnlyStorePosition /* false */)
: CDialogEx(nIDTemplate, pParent)
, m_bDoNotShowResizeIcon(false)
, m_bOnlyStorePosition(bOnlyStorePosition)
, m_bLimitToHorizontalResizing(false)
, m_strWindowID(strWindowID)
{
m_rcInit.SetRect(0, 0, 0, 0);
}
CBaseEditor::CBaseEditor() noexcept(false)
{
Init();
}
答案1
得分: 1
第一步
将我的对话构造函数更改为将this
传递给CBaseEditor
:
CPublishersDatabaseViewerDlg::CPublishersDatabaseViewerDlg(CWnd* pParent /*=nullptr*/) noexcept(false)
: CResizingDialog(L"GroupsReport", IDD_DIALOG_PUBLISHERS_DATABASE_VIEWER, pParent)
, CBaseEditor(this)
, m_strPreviewXSL(L"")
{
m_strPreviewXSL = theApp.GetStringSetting(L"Options", L"PublisherDB_Style", L"PublisherDB--Field Service Groups");
}
第二步
在基本编辑器中添加一个新的CWnd*
变量,并更新构造函数:
CBaseEditor::CBaseEditor(CWnd* pParent /* nullptr */) noexcept(false)
{
m_pParentWnd = pParent;
Init();
}
第三步
更改ImportTemplate
函数,因为它不再需要传递父窗口。我们可以使用m_pParentWnd
代替。
英文:
Step 1
Change my dialog constructor to feed in the this
to the CBaseEditor
:
CPublishersDatabaseViewerDlg::CPublishersDatabaseViewerDlg(CWnd* pParent /*=nullptr*/) noexcept(false)
: CResizingDialog(L"GroupsReport", IDD_DIALOG_PUBLISHERS_DATABASE_VIEWER, pParent)
, CBaseEditor(this)
, m_strPreviewXSL(L"")
{
m_strPreviewXSL = theApp.GetStringSetting(L"Options", L"PublisherDB_Style", L"PublisherDB--Field Service Groups");
}
Step 2
Add a new CWnd*
variable to the base editor and update the constructor:
CBaseEditor::CBaseEditor(CWnd* pParent /* nullptr */) noexcept(false)
{
m_pParentWnd = pParent;
Init();
}
Step 3
Change the ImportTemplate
function as it no longer needs the parent passed in. We can use the m_pParentWnd
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论