Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

c++ - Can a custom MFC window/dialog be a class template instantiation?

There's a bunch of special macros that MFC uses when creating dialogs, and in my quick tests I'm getting weird errors trying to compile a template dialog class. Is this likely to be a big pain to achieve?

Here's what I tried:

MyDlg.h

template <class W>
class CMyDlg : public CDialog
{
    typedef CDialog super;
    DECLARE_DYNAMIC(CMyDlg <W>)

public:
    CMyDlg (CWnd* pParent);   // standard constructor
    virtual ~CMyDlg ();

// Dialog Data
    enum { IDD = IDD_MYDLG };

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()

private:
    W *m_pWidget; //W will always be a CDialog
};



IMPLEMENT_DYNAMIC(CMyDlg<W>, super) <-------------------

template <class W>
CMyDlg<W>::CMyDlg(CWnd* pParent)
    : super(CMyDlg::IDD, pParent)
{
  m_pWidget = new W(this);
}

I get a whole bunch of errors but main one appears to be:

error C2955: 'CMyDlg' : use of class template requires template argument list

I tried using some specialised template versions of macros but it doesn't help much, other errors change but this one remains. Note my code is all in one file, since C++ templates don't like .h/.cpp like normal.

I'm assuming someone must have done this in the past, possibly creating custom versions of macros, but I can't find it by searching, since 'template' has other meanings.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may have other problems as well, but one thing has got to be your use of super. That's a java thing not a C++ thing. Instead of super you need to use CDialog.

After looking into IMPLEMENT_DYNAMIC the macro definition is not compatible with templates, it doesn't use the template <class T> syntax before the function definitions. What you need to do is define derived class specializations of your template and then use the macro on them. So you could do this:

class MyDlgA : public CMyDlg<A>
{
};

IMPLEMENT_DYNAMIC(MyDlgA, CDialog);

And then do that for all specializations that you want. If that's not feasible, look at the macro and make your own templatized version of it.

Edit: Following up on my comment, you could make a macro like this:

#define INSTANTIATE_DLG_TEMPLATE(T)  
class MyDlg##T : public CMyDlg<T>    
{                                    
};                                   
                                     
IMPLEMENT_DYNAMIC(MyDlg##T, CDialog);

And then just use this wherever you would normally have defined the template specialization in a header file with a typedef.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...