Calling non-template base class constructor in class derived from templated intermediary
template <class WndClass>
class screenhelpers : public WndClass
{
public:
typedef WndClass BaseClass;
typedef typename screenhelpers<WndClass> ThisClass;
CRect GetControlRect(CWnd *pControl) const
{
CRect rectWindow(0,0,0,0);
if (!pControl)
return rectWindow;
pControl->GetWindowRect(&rectWindow);
this->ScreenToClient(&rectWindow);
return rectWindow;
}
};
class MyDialog : public screenhelpers<CDialog>
{
public:
typedef screenhelpers<CDialog>::BaseClass MDialog;
MyDialog(int i);
MyDialog(LPCSTR lpszTemplateName, CWnd *pParentWnd);
MyDialog(int nIDTemplate, CWnd *pParentWnd);
};
MyDialog::MyDialog(int i)
{
BaseClass b;
}
MyDialog::MyDialog(LPCSTR lpszTemplateName, CWnd *pParentWnd)
: MyDialog::BaseClass(lpszTemplateName, pParentWnd)
{
}
MyDialog::MyDialog(int nIDTemplate, CWnd *pParentWnd)
: MyDialog::CDialog(nIDTemplate, pParentWnd)
{
}
I don't see why I cannot seem to call the base class of screenhelpers.
If MyDialog inherits from screenhelpers, and s开发者_StackOverflow中文版creenhelpers inherits from CDialog, why can't I call CDialog?
The initialization list in the constructor can only call its immediate parent's constructor, not one further up the chain. That's why you can't initialize CDialog directly.
Your screenhelpers class doesn't define a constructor that takes two parameters, so that's not going to work either. Even if you add such a constructor, I'm not sure it's valid syntax to refer to it by the typedefed name, you might need to use screenhelpers<CDialog>
instead.
If MyDialog
constructor were allowed to call CDialog
constructor, the latter would be called twice: once by MyDialog
and once by screenhelpers
. That would be a disaster.
If you need to control how CDialog
constructor is called from MyDialog
, you need to use virtual inheritance:
template <class WndClass>
class screenhelpers : public virtual WndClass
Then you will have (not just be able) to call CDialog
constructor from MyDialog
.
Note that this may have other effects on your design.
精彩评论