Re: A generic pointerOfMethod?
Hi Nick
"nick" <nick@discussions.microsoft.com> wrote in message
news:9A884872-7465-4364-976E-FB8C5C259C30@microsoft.com...
These methods in A are very similar. But some may have int parameter while
others have double, and a couple of them with string parameter... The big
classes is written by others.
I hope I can defined a generic algorithm to consume the big class. So in
my
implement, the only change part is a mapping between array of B and A's
method....
You can pobably design something with intermediate classes and virtual
functions to achieve this:
class BaseDelegate
{
A* m_pA;
public:
BaseDelegate(A* pa) : m_pA(pA) {}
virtual void Invoke(int) = 0;
};
typedef void (A::*PMFNINT)(int);
class IntDelegate : BaseDelegate
{
PMFNINT m_pMFN;
public:
IntDelegate(m_pA, PMFNINT pMFN) : BaseDelegate(m_pA), m_pMFN(pMFN) {}
void Invoke(int i) { m_pA->*pMFN(i); }
};
typedef void (A::*PMFNINT)(double);
class DoubleDelegate : BaseDelegate
{
PMFNINT m_pMFN;
public:
DoubleDelegate(m_pA, PMFNINT pMFN) : BaseDelegate(m_pA), m_pMFN(pMFN) {}
void Invoke(int i) { m_pA->*pMFN(i); }
};
Your calling class can then have a member variable of type BaseDelegate that
points either to an IntDelegate or a DoubleDelegate. In both bases the call
would be
pBaseDelegate->Invoke(42);