In one of my projects I’m using the same CRTP approach (deriving from enable_crtp) as in Answer 1 here: How do I pass template parameters to a CRTP?
However I have the need to derive from the derived classes too. Is there any way to make this work with out falling back to just static_cast the this pointer but by using the self() method from the Enable CRTP base class?
#include "EnableCRTP.h"
template<typename DERIVED>
class BASE : public EnableCRTP<DERIVED>
{
friend DERIVED;
public:
void startChain()
{
self()->chain();
}
};
template<typename DERIVED>
class Derived1 : public BASE<Derived1<DERIVED> >
{
public:
void chain()
{
std::cout << "Derived1" << std::endl;
//self()->chain2(); <- compile Error
static_cast<DERIVED*>(this)->chain2(); // <-Works
}
};
class Derived2 : public Derived1<Derived2>
{
public:
void chain2()
{
std::cout << "Derived2" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Derived2 der;
der.startChain();
return 0;
}
You can give the most-derived class as the template parameter to the CRTP base class, so that it will have access to all its members. Instead of
Use:
There are also other issues with your code. For example, you can’t directly call
self()like you do because the compiler doesn’t know thatselfis a member of the base class (which is dependent on a template parameter). Instead, callthis->self(). See this FAQ entry.