This is more a best practice question than a language question in itself, since I already have a working solution to what seems to be a common stumbling block in C++.
I’m dealing with a typical cyclic dependency issue in template parameter substitutions. I have the following pair of classes:
template<class X>
class A { /* ... */ };
template<class X>
class B { /* ... */ };
and I want to instantiate each one as the following:
// Pseudocode -- not valid C++.
A<B> a;
B<A> b;
that is, I want to ‘bind’ A to B, and B to A.
I can solve the problem, in a gross way, through a forward declaration with inheritance trick:
class sA;
class sB;
class sA : public A<sB> { /* ... */ };
class sB : public B<sA> { /* ... */ };
but this brings in a set of problems, since sA and sB are not indeed A and B. For example, I cannot invoke A‘s constructors without properly duplicating them into sA, or somehow sparkling casts around the code.
My question is: what is the best practical way to deal with this issue? Any specially clever solution to this problem?
I am using both MSVC2008 and G++, but solutions with compiler-specific extensions are welcome.
Thanks,
Alek
Since a template’s type names all its parameters, you can’t have an endless loop of parameterization.
You are probably (certainly) just trying to send information in opposite directions at the same time. There’s no problem with that, but you can’t encapsulate the information in the classes that provide implementation.