I need to solve such a problem. There is a base class and two inherited classes. The base class contains method which needs a function-pointer as a parameter. But such functions are defined in inherited classes.
class CBase;
typedef bool (CBase::*FPredicate)();
class CBase
{
public:
CBase() {}
~CBase() {}
protected:
//this method waits until 'predicate' is true or until 'timeout' ms. passed
//and returns true if 'predicate' is true eventually
bool WaitEvent(FPredicate predicate, int timeout)
{
bool result = false;
int time1 = GetTickCount();
int time2;
bool isEnd = false;
while(!isEnd)
{
result = isEnd = (this->*predicate)();
time2 = GetTickCount();
if(time2 - time1 > timeout && !isEnd)
isEnd = true;
}
return result;
}
};
class CChildA : public CBase
{
protected:
bool a1() {/*some work*/}
bool a2() {/*some work*/}
void a_main()
{
...
WaitEvent(&CChildA::a1, 100);
...
WaitEvent(&CChildA::a2, 100);
...
}
};
class CChildB : public CBase
{
protected:
bool b1() {/*some work*/}
bool b2() {/*some work*/}
void b_main()
{
...
WaitEvent(&CChildB::b1, 100);
...
WaitEvent(&CChildB::b2, 100);
...
}
};
MSVC 2005 compiler gives an error on WaitEvent calls:
error C2664: 'CBase::WaitEvent' : cannot convert parameter 1 from 'bool (__thiscall CChildA::* )(void)' to 'FPredicate'
A question is: how shall I change the code to make it work? will it be safe to rewrite WaitEvent call as
WaitEvent((FPredicate)(&CChildA::a1), 100)
?
In this case compiler tells of no error but is it safe? Or is there a better way of solving a problem?
Thank you in advance.
The problem is that the implicitly passed this differs in type. Either you cast it, but that will probably fail in the presence of multiple inheritance. A better & more robust solution would be to change the signature to:
template< typename T >
bool WaitEvent( bool ( T::*predicate )(), int timeout ) { ... }
You can do it using a template class to do a closure of your child object and its function member saving it's correct type. And then using virtual functions to let the base class calls it through usual polymorphism.
A similar mechanism is used in shared_ptr to call destructors. See: http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-1-of-n
#include <iostream>
struct CPredicateBase
{
virtual ~CPredicateBase() {}
virtual bool operator()() = 0;
};
template <class T>
struct CPredicate : public CPredicateBase
{
bool (T::*func)();
T* self;
CPredicate(T* self_, bool (T::*func_)())
: func(func_), self(self_) {}
bool operator() () { return (self->*func)(); }
};
class CBase
{
public:
bool WaitEvent(CPredicateBase& predicate, int imeout)
{
/// just to show the call
bool b = predicate();
std::cout << "WaitEvent called predicate() => " << b << std::endl;
return b;
}
};
class CChildA : public CBase
{
public:
bool a1() { return false; }
bool a2() { return true; }
void a_main()
{
std::cout << "CChildA::a_main()" << std::endl;
CPredicate<CChildA> caller1(this, &CChildA::a1);
bool ra1 = WaitEvent(caller1, 100);
CPredicate<CChildA> caller2(this, &CChildA::a2);
bool ra2 = WaitEvent(caller2, 100);
}
};
class CChildB : public CBase
{
public:
bool b1() { return false; }
bool b2() { return true; }
void b_main()
{
std::cout << "CChildB::b_main()" << std::endl;
CPredicate<CChildB> caller1(this, &CChildB::b1);
bool rb1 = WaitEvent(caller1, 100);
CPredicate<CChildB> caller2(this, &CChildB::b2);
bool rb2 = WaitEvent(caller2, 100);
}
};
int main(int argc, char const* argv[])
{
CChildA cA;
CChildB cB;
cA.a_main();
cB.b_main();
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With