Solved

Template in RUNTIME_CLASS macro

Posted on 1998-11-16
9
957 Views
Last Modified: 2013-11-20
I've declared a template class which looks like this:

template <class INPUT_WND_TYPE>
CInpWnd<INPUT_WND_TYPE>::CInpWnd() : INPUT_WND_TYPE()
{
...
}

Then later, I pass a class name to the RUNTIME_CLASS macro like this:

RUNTIME_CLASS(CInpWnd<CSaisieView>)

When I compile, I get this error message:

C:\Windows\Bureau\Saisie\Saisie.cpp(78) : error C2039: 'classCInpWnd' : is not a member of 'CInpWnd<class CSaisieView>'

It seems that a parameter of type 'aaa<bbb>' passed to a macro has only the 'aaa' part being read.

Can someone help me with this.  The use of templates for this situation in my program makes my code much smaller and more generic.
0
Comment
Question by:jhendrix
  • 4
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1324775
Did you provide the DECLARE_DYNCREATE()/IMPLEMENT_DYNCREATE() macros in your temlate header file? The error you're getting usually is a result when omitting these...
0
 

Author Comment

by:jhendrix
ID: 1324776
Comment for JKR:

I cannot use these macros and I hope to be clear on this cause I might not be able to explain this well.

Here is my template class:

     template <class INPUT_WND_TYPE>
     CInpWnd<INPUT_WND_TYPE>::CInpWnd() : INPUT_WND_TYPE()
     {    
     ...
     }

I cannot use DECLARE_DYNCREATE(CInpWnd) here cause CInpWnd is not a class and will never be one.  CInpWnd<CMyDialog> and CInpWnd<CMyView> will be classes for example that's when the INPUT_WND_TYPE part of the template will be resolved.
CInpWnd<INPUT_WND_TYPE> is a template class which derives itself from class  INPUT_WND_TYPE, which will be any MFC class derived from CObject.  

Now in the InitInstance() function of my program, the AppWizard inserts:

      pDocTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CMyDoc),
            RUNTIME_CLASS(CMainFrame),       // main SDI frame window
//            RUNTIME_CLASS(CMyView)
// I replaced the above line by this next line for the purpose of my program
            RUNTIME_CLASS(CInpWnd<CMyView>)
            );
      AddDocTemplate(pDocTemplate);

The compiler here then takes the macro parameter 'CInpWnd<CMyView>' and truncates the '<CMyView>' part.  This is one part of my problem.

The other and more general part is how do I use the xxx_DYNCREATE stuff on CInpWnd<CMyDiaog> and CInpWnd<CMyView>.  Where and how can I use these 3 lines:

DECLARE_DYNAMIC(CInpWnd<CMyView>)
IMPLEMENT_DYNAMIC(CInpWnd<CMyView>)
RUNTIME_CLASS(CInpWnd<CMyView>)

0
 

Author Comment

by:jhendrix
ID: 1324777
Adjusted points to 100
0
 
LVL 86

Expert Comment

by:jkr
ID: 1324778
Well, the first problem is that (in order to use the 'RUNTIME_CLASS()' macros) your class must be derived from 'CObject' (otherwise the instantiation of 'CSingleDocTempate
' will fail anyway...) - which a template class can't be (by nature). The macros i mentioned expand to (e.g., in your case)
protected:
      static CRuntimeClass* PASCAL _GetBaseClass();
public:
      static const AFX_DATA CRuntimeClass classCInpWnd<CMyView>; // <-- this one is missing
      virtual CRuntimeClass* GetRuntimeClass() const;

//.cpp
      const AFX_DATADEF CRuntimeClass class_name::classCInpWnd<CMyView> = {
            CInpWnd<CMyView>, sizeof(class CInpWnd<CMyView>), wSchema, pfnNew,
                  RUNTIME_CLASS(base_class_name), NULL };
      CRuntimeClass* CInpWnd<CMyView>::GetRuntimeClass() const
            { return RUNTIME_CLASS(class_name); }

where CRuntimeClass is mainly a structure holding some administrative data... (phew! <s>)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:GGRUNDY
ID: 1324779
Macro expansion happens before template expansion and
The DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros on which RUNTIME_CLASS depend upon the
macro concat ## operation. This makes things difficult.

I reckon the best work-a-round for your problem is to
wrap your template definition in a MACRO which will
handle the DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC
macros for you.
e.g.
//-------------------- your template -------------------
//-------------------- or something similar ---------
template <class INPUT_WND_TYPE>
class CInpWnd: public INPUT_WND_TYPE
{
  public:
  void OnDraw(class CDC *);
  };

//------------------ the wrapping macros -------------
//------------------- in a header ----------------
#define DECLARE_TEMPLATE_DYNAMIC(template_name, base_class_name) \
class template_name##base_class_name : public template_name<base_class_name> \
{ \
  DECLARE_DYNAMIC(template_name##base_class_name); \
  }

#define IMPLEMENT_TEMPLATE_DYNAMIC(template_name, base_class_name) \
  IMPLEMENT_DYNAMIC(template_name##base_class_name, base_class_name);

//----------------- call the macros
//----------------- in your c code somewhere -------------
DECLARE_TEMPLATE_DYNAMIC(CInpWnd, CView);
IMPLEMENT_TEMPLATE_DYNAMIC(CInpWnd, CView);

//----------------- and then you can declare instances
//----- NOTE not CInpWnd<CView > but CInpWndCWiew

CInpWndCView view;

//---------------------------------
Cheers Greg

void f()
{
  RUNTIME_CLASS(CInpWndCView);
  }

0
 
LVL 86

Expert Comment

by:jkr
ID: 1324780
Greg - the problem is that in order to get dynamic creation working the classes that use it will have to be derived from CObject - expanding the macros isn't difficult at all (simply add '/P' to the compiler switches to see the preprocessed output)
0
 

Author Comment

by:jhendrix
ID: 1324781
Thanx GGRUNDY,

I always like simple solutions that work, although I'm kinda disappointed in myself for not being able to come up with it.  I'll be going to sleep a bit smarter tonight.
This actually solved another problem I was probably getting because of the way I was trying to do this.

I don't know how this experts-exchange stuff works but why didn't you use the 'answer' button for giving me the solution?  There's 100 points to be had.
0
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 100 total points
ID: 1324782
Answer as above
0
 

Author Comment

by:jhendrix
ID: 1324783
Thanx GGRUNDY,

I always like simple solutions that work, although I'm kinda disappointed in myself for not being able to come up with it.  I'll be going to sleep a bit smarter tonight.
This actually solved another problem I was probably getting because of the way I was trying to do this.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now