Click here to Skip to main content
15,915,611 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPropertyPage Pin
shakumar_2213-Mar-09 6:08
shakumar_2213-Mar-09 6:08 
AnswerRe: PropertyPage Pin
Code-o-mat13-Mar-09 6:38
Code-o-mat13-Mar-09 6:38 
GeneralRe: PropertyPage Pin
shakumar_2214-Mar-09 2:31
shakumar_2214-Mar-09 2:31 
GeneralRe: PropertyPage Pin
Code-o-mat14-Mar-09 4:01
Code-o-mat14-Mar-09 4:01 
QuestionCreating derived type from a base class template Pin
Skippums13-Mar-09 6:00
Skippums13-Mar-09 6:00 
AnswerRe: Creating derived type from a base class template Pin
Stuart Dootson13-Mar-09 6:06
professionalStuart Dootson13-Mar-09 6:06 
GeneralRe: Creating derived type from a base class template Pin
Skippums13-Mar-09 6:15
Skippums13-Mar-09 6:15 
GeneralRe: Creating derived type from a base class template Pin
Stuart Dootson13-Mar-09 6:40
professionalStuart Dootson13-Mar-09 6:40 
I think the g++ error messages are clearer - given a file b.cpp:

template <class T>
class Measure
{
public:
   double Value;
   typename T::UnitList Units;

   virtual const T& Convert(const typename T::UnitList destUnits) = 0;
    // Declare a number of other pure virtual functions
};

// Length Declaration in "Length.h"
class Length : public Measure<Length>
{
public:
   enum UnitList
   {
      METERS,
      MILES,
   };
   virtual const Length& Convert(const UnitList destUnits)
   {

   }
};


I get these error messages

b.cpp: In instantiation of ‘Measure<Length>’:
b.cpp:12:   instantiated from here
b.cpp:5: error: invalid use of undefined type ‘class Length’
b.cpp:12: error: forward declaration of ‘class Length’
b.cpp:7: error: invalid use of undefined type ‘class Length’
b.cpp:12: error: forward declaration of ‘class Length’


It's trying to instantiate the base class before defining Length. But that requires the compiler to have a definition for Length::UnitList...but it can't have that until Length is defined. Which can't happen until Measure<Length> is defined!

So yeah, that's not gonna work!

You can use the CRTP[^] idiom for static polymorphism, like this - Measure will call the Flip method of the template parameter. It's really useful for implementing mix-ins[^], as used by ATL and WTL.
template <class T>
class Measure
{
public:
   double Value;

   virtual const T& Convert(const int destUnits)
   {
      static_cast<T*>(this)->Flip();
   }
    // Declare a number of other pure virtual functions
};

// Length Declaration in "Length.h"
class Length : public Measure<Length>
{
public:
   enum UnitList
   {
      METERS,
      MILES,
   };
   virtual const Length& Convert(const UnitList destUnits)
   {
      Measure<Length>::Convert(1);
   }
   void Flip() {}
};


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: Creating derived type from a base class template Pin
Skippums13-Mar-09 8:12
Skippums13-Mar-09 8:12 
GeneralRe: Creating derived type from a base class template Pin
Stuart Dootson13-Mar-09 14:02
professionalStuart Dootson13-Mar-09 14:02 
GeneralRe: Creating derived type from a base class template Pin
Skippums19-Mar-09 9:40
Skippums19-Mar-09 9:40 
QuestionMultiByteToWideChar crashes out on longer strings [modified] Pin
RichardBrock13-Mar-09 4:45
RichardBrock13-Mar-09 4:45 
AnswerRe: MultiByteToWideChar crashes out on longer strings Pin
led mike13-Mar-09 5:37
led mike13-Mar-09 5:37 
GeneralRe: MultiByteToWideChar crashes out on longer strings Pin
RichardBrock13-Mar-09 6:22
RichardBrock13-Mar-09 6:22 
QuestionRe: MultiByteToWideChar crashes out on longer strings Pin
led mike13-Mar-09 7:40
led mike13-Mar-09 7:40 
AnswerRe: MultiByteToWideChar crashes out on longer strings Pin
RichardBrock13-Mar-09 8:00
RichardBrock13-Mar-09 8:00 
GeneralRe: MultiByteToWideChar crashes out on longer strings Pin
led mike13-Mar-09 8:41
led mike13-Mar-09 8:41 
AnswerRe: MultiByteToWideChar crashes out on longer strings Pin
Akt_4_U13-Mar-09 5:39
Akt_4_U13-Mar-09 5:39 
GeneralRe: MultiByteToWideChar crashes out on longer strings Pin
RichardBrock13-Mar-09 6:26
RichardBrock13-Mar-09 6:26 
Questionget the font of my control and set it with an other (the face name) Pin
MrKBA13-Mar-09 4:26
MrKBA13-Mar-09 4:26 
AnswerRe: get the font of my control and set it with an other (the face name) Pin
Code-o-mat13-Mar-09 5:15
Code-o-mat13-Mar-09 5:15 
GeneralRe: get the font of my control and set it with an other (the face name) Pin
MrKBA13-Mar-09 5:30
MrKBA13-Mar-09 5:30 
GeneralRe: get the font of my control and set it with an other (the face name) Pin
Code-o-mat13-Mar-09 5:39
Code-o-mat13-Mar-09 5:39 
GeneralRe: get the font of my control and set it with an other (the face name) Pin
MrKBA13-Mar-09 5:47
MrKBA13-Mar-09 5:47 
GeneralRe: get the font of my control and set it with an other (the face name) Pin
Code-o-mat13-Mar-09 5:57
Code-o-mat13-Mar-09 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.