I have a class that uses an "add-on" template to add additional functionality as below:
template< class T >
class AddOn_A
{
public:
int SomeFuncA()
{
T* pT = static_cast< T* >( this );
return pT->DoSomething() + 1;
};
};
class CMyClass : public AddOn_A< CMyClass >
{
public:
int DoSomething()
{
return 100;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
CMyClass A;
_ASSERT( A.SomeFuncA() == 101 );
return 0;
}
Now I would like to extend this such that CMyClass can accept different add-ons like AddOn_B.
template< class T >
class AddOn_B
{
public:
int SomeFuncB()
{
T* pT = static_cast< T* >( this );
return pT->DoSomething() + 2;
};
};
template< class AddOn >
class CMyClass : public AddOn
{
public:
int DoSomething()
{
return 100;
};
};
int _tmain(int argc, _TCHAR* argv[])
{
// error C3203: 'AddOn_A' : unspecialized class template can't be used as a template argument for template parameter 'AddOn', expected a real type
// error C2955: 'AddOn_A' : use of class template requires template argument list
CMyClass< AddOn_A > A;
_ASSERT( A.SomeFuncA() == 101 );
// same errors here
CMyClass< AddOn_B > B;
_ASSERT( B.SomeFuncB() == 102 );
return 0;
}
Unfortunately, each Add_On requires CMyClass as a template parameter which requires an Add_On, etc... I'm in a requirement loop.
Is there some template magic I can use to get the functionality I'm looking for? Is there a better method of doing this?
Thanks, PaulH