Re: Resolving template parameters
John Harrison wrote:
This works for me, and it isn't retricted to a known set of template
parameters. But it doesn't pass the member function to be called to the
call method. I wasn't sure if that was part of your requirements or not.
john
#include <iostream>
class B;
class Base
{
public:
virtual void call(B* b) = 0;
};
class B
{
public:
void x(Base *pSomePtr)
{
pSomePtr->call(this);
};
template <class T1,class T2>
void y()
{
std::cout << typeid(T1).name() << ' ' << typeid(T2).name() << '\n';
};
};
template <class T1, class T2>
class A : public Base
{
public:
void call(B* b)
{
b->y<T1, T2>();
}
};
int main()
{
A<int, double> a;
B b;
b.x(&a);
}
Darn, you beat me to it!! :) Interestingly enough, we came up
with the same solution. BTW, I forgot (so I went and included
it in my solution) do you need to do ->template in the call
to member function y()? (See below).
--------------------------------------------------------------
// Modifying your example and removing ellipsis since
// it is too vague for me to work with. WLOG,
// assuming all your functions are have void arguments.
// forward declaration required.
class B;
// Good Stuff here. Keep
class Base
{
public:
// I am deducing you want this to apply a member function to
// an instance of class B
virtual void call( B *b ) =0;
}
// had to move the definition of B up due to dependency.
class B
{
public:
void x();
template <class T1,class T2>
void y()
{
T1 *a = new T1();
T2 *b = new T2();
// leak memory!! WooHoo!
};
};
template <class T1,class T2>
class A : public Base
{
public:
// this needs to be virtual right?
virtual void call( B *b )
{
// you do not need to switch on typeid. since the virtual
// function will do it for you automagically I am also assuming
// that this "call" function will call y.
// if you need to remap T1 and T2 to some other type
// do NOT switch on typeid, use a template type function instead.
b->template y<T1,T2>();
}
}
inline void B::x()
{
Base *pSomePtr = new A<int, float>();
pSomePtr->call( this );
// WooHoo!! Leak more memory!!
};
// Honestly though, with such a cyclic dependency between
// class A and B, are you sure you want to do this at all?