Solved

Template in RUNTIME_CLASS macro

Posted on 1998-11-16
9
953 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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 100
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:GGRUNDY
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Answer as above
0
 

Author Comment

by:jhendrix
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

13 Experts available now in Live!

Get 1:1 Help Now