【发布时间】:2011-10-05 12:24:41
【问题描述】:
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)
{
}
我不明白为什么我似乎无法调用屏幕助手的基类。
如果MyDialog继承自screenhelpers,screenhelpers继承自CDialog,为什么不能调用CDialog?
【问题讨论】:
-
我有另一个简化的例子
class Bob { public: Bob() : t(12){} Bob(int i) : t(i){} int t; }; template <class Base> class Bill : public Base { public: Bill() : s(20){} typedef Base BaseClass; int s; }; class it : public Bill<Bob> { public: it(int i) { s = i; t= 1;} //Create equivalent of above line that explicitly intialises the t that resides in Bob #if 1//My unworking attempt it (double d) : BaseClass(30) {} #endif }; -
it 构造函数正在修改 Bob 的成员 t,从而证明基类 Bob 存在于其中
-
如果您指出您遇到的错误会有所帮助,我不得不在我的答案中猜测。
-
哎呀。当尝试使用 typedef
error C2614: 'MyDialog' : illegal member initialization: 'BaseClass' is not a base or member -
当尝试使用 CDialog
error C2614: 'MyDialog' : illegal member initialization: 'CDialog' is not a base or member
标签: c++ templates mfc constructor base-class