I'm building a small template hierarchy and try to make use of class polymorphism. Here's some example code (which does not compile) to demonstrate it:
template<typename T>
struct A
{};
template<typename T>
struct B
{
B (A<B> *) {}
};
struct C : public B<int>
{
C(A<C> *p) : B<int>(p) {} // error
};
int main() {
A<C> ac;
C c(&ac);
}
My compiler (gcc 4.01 on OS X) throws the following error on the specified line:
error: no matching function for call to ‘B<int>::B(A<C>*&)’
But from my logical assumptions the code should work because
C == B<int>
B<int> == B<T>
=> A<C> == A<B<T> >
Can someone point out where my mistake lies and how to fix it, please?
EDIT
The answer seems to be that my problem can not be solved by regular inheritance trees because templates are to static to recognize polymorphism. Would it be legal then to cast A< C > to A< B < int > > to force polymorphism although it's horrible from a design standpoint?
struct C : public B<int>
{
C(A<C> *p) : B<int>((A<B<int> > *)p) {}
};
Is there any "good" solution for this problem?