在从模板化中介派生的类中调用非模板基类构造函数
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?
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 screenhelpers inherits from CDialog, why can't I call CDialog?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构造函数中的初始化列表只能调用其直接父级的构造函数,而不能调用链上一级的构造函数。这就是为什么你不能直接初始化CDialog。
您的 screenhelpers 类没有定义带有两个参数的构造函数,因此这也不起作用。即使您添加这样的构造函数,我也不确定通过类型定义名称引用它是否是有效的语法,您可能需要使用
screenhelpers
代替。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.如果允许
MyDialog
构造函数调用CDialog
构造函数,则后者将被调用两次:一次由MyDialog
调用,一次由screenhelpers
调用代码>.那将是一场灾难。如果您需要控制如何从
MyDialog
调用CDialog
构造函数,则需要使用虚拟继承:那么您将拥有(不仅仅是能够) 从
MyDialog
调用CDialog
构造函数。请注意,这可能会对您的设计产生其他影响。
If
MyDialog
constructor were allowed to callCDialog
constructor, the latter would be called twice: once byMyDialog
and once byscreenhelpers
. That would be a disaster.If you need to control how
CDialog
constructor is called fromMyDialog
, you need to use virtual inheritance:Then you will have (not just be able) to call
CDialog
constructor fromMyDialog
.Note that this may have other effects on your design.