This seems to work, but I'm not 100% certain it's legal and would appreciate some feedback.
I have a subclass that is derived from a generic base class. It's similar to the curiously recurring template pattern, but different.
In derived.h:
template <class T>
class Derived : public T
{
public:
Derived();
... and some other stuff ...
};
In derived.cpp:
#include "derived.h"
template <class T>
Derived<T>::Derived()
{
...
}
// defining the variations I need here avoids linker errors
// see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
template Derived<Base1>::Derived();
template Derived<Base2>::Derived();
And in some other file:
#include "derived.h"
void test()
{
Derived<Base1> d1;
Derived<Base2> d2;
... do stuff with d1 and d2 ...
}
Yes, you can derive from any class, including one that is (or is dependent on) a template argument.
As noted in the comments, it must be a complete type; that is, it must have been defined before the template is instantiated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With