本文介绍了扩展模板类(添加构造函数),是否可能!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果:If:template <class TBase>class COfficeBorder : public TBase{protected:virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam){switch (message){case WM_NCPAINT:{TBase::WindowProc(message, wParam, lParam);...return 0; // Handled.}break;case WM_CREATE:{if (TBase::WindowProc(message, wParam, lParam) == -1)return -1;...return 0; // Handled.}break;case WM_NCCALCSIZE:{LRESULT lResult = TBase::WindowProc(message, wParam, lParam);...return lResult;}break;}return TBase::WindowProc(message, wParam, lParam);}}; 是一个模板类,已声明没有任何构造函数正如你所看到的那样。 &我需要通过添加构造函数来扩展它。 所以这是扩展模板类:is a template class which has been declared without any constructor as you can see.& I need to extend it by adding the constructor to it.So this is the extended template class:template <class TBase>class COfficeBorderEx : public COfficeBorder<TBase>{protected:COfficeBorderEx(UINT nIDTemplate) : TBase(nIDTemplate){}}; 所以我不必重写 COfficeBorder 代码。只有构造函数 我班上的问题,这是一个 CFormView 派生类: b $ bSo I don't have to rewrite the COfficeBorder code. only the constructorThe problem in my class, which is a CFormView derived class:class CMyView : public COfficeBorderEx<CFormView>{protected: CMyView();DECLARE_DYNCREATE(CMyView)public://{{AFX_DATA(CMyView)enum{ IDD = IDD_MYVIEW_FORM };// NOTE: the ClassWizard will add data members here//}}AFX_DATA...}; 它的构造函数需要调用基类构造函数,如下所示:Its constructor requires calling the base class constructor as the below:CMyView::CMyView() : CFormView(CMyView::IDD){ //{{AFX_DATA_INIT(CMyView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // TODO: add construction code here} 问题在于调用 CFormView(CMyView) :: IDD),它调用: COfficeBorderEx(CMyView :: IDD),这最后调用: TBase(CMyView :: IDD)实际上是: COfficeBorder(CMyView :: IDD) 和 COfficeBorder 根本没有构造函数。 有没有办法可行这个没有从头开始重写 COfficeBorderEx 模板类并将构造函数嵌入其中!? 感谢您的理解。The problem is when calling CFormView(CMyView::IDD), it calls: COfficeBorderEx(CMyView::IDD), which this last calls: TBase(CMyView::IDD) which is actually: COfficeBorder(CMyView::IDD)And COfficeBorder Doesn't have a constructor at all.Is there a possible way for doing this without re-writing the COfficeBorderEx template class from the beginning and embed a constructor to it !?Thank you for your understanding.推荐答案 这个的主要目的是代码压缩(写更少的代码。) 而不是写这个(工作得非常好): 模板< class tbase =>The main purpose for this is code compression (write less code.)instead of writing this (which works very fine):template <class tbase="">class COfficeBorderEx : public TBase{protected:COfficeBorderEx(UINT nIDTemplate) : TBase(nIDTemplate) // This is the constructor which I need.{}virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam){switch (message){case WM_NCPAINT:{TBase::WindowProc(message, wParam, lParam);...return 0; // Handled.}break;case WM_CREATE:{if (TBase::WindowProc(message, wParam, lParam) == -1)return -1;...return 0; // Handled.}break;case WM_NCCALCSIZE:{LRESULT lResult = TBase::WindowProc(message, wParam, lParam);...return lResult;}break;}return TBase::WindowProc(message, wParam, lParam);}}; 我会写这个(这对我不起作用) ):I would write just this (which didn't worked for me):template <class TBase>class COfficeBorderEx : public COfficeBorder<TBase>{protected:COfficeBorderEx(UINT nIDTemplate) : TBase(nIDTemplate){}}; 但是我收到的编译错误说: 错误C2512:'COfficeBorderEx':没有合适的默认构造函数 我猜它是不可能的:(But I get a compiler error that says:error C2512: 'COfficeBorderEx' : no appropriate default constructor availableI guess It is not possible :( 这篇关于扩展模板类(添加构造函数),是否可能!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-26 10:35