Polymorphic Boost Function
Dear all, Is it possible for boost function to store a polymorphic function like this.
Code:
typedef boost::function<encryptionStrategy* (encryptionStrategyFactory* ) > factoryFn;
std::vector<factoryFn> factoryCont;
factoryCont.push_back(&encryptionStrategyFactory::createEncryptor);
factoryCont.push_back(&AESEncryptionFactory::createEncryptor);
factoryCont.push_back(&RSAEncryptionFactory::createEncryptor);
The AESEncryptionFactory and RSAEncryptionFactory are inherited from base class encryptionStrategyFactory but i encounter like conversion problem.
Is the polymorphic behaviour supports in Boost Function ?
Please help.
Thanks.
Re: Polymorphic Boost Function
Virtual member-function pointers are called polymorphically - evn through a boost::function object.
You have to have an object instance to call a member function pointer:
Code:
#include <boost/function.hpp>
#include <iostream>
#include <vector>
using namespace std;
struct A
{
virtual void foo() {cout << "A::foo" << endl;}
};//A
struct B : public A
{
virtual void foo() {cout << "B::foo" << endl;}
};//B
struct C : public A
{
virtual void foo() {cout << "C::foo" << endl;}
};//C
int main()
{
A a;
B b;
C c;
typedef boost::function<void (A*)> Fn;
vector<Fn> vFn;
vFn.push_back(&A::foo);
vFn[0](&a);
vFn[0](&b);
vFn[0](&c);
return 0;
}//main
So you only need to push_back a &encryptionStrategyFactory::createEncryptor. Then use the factory instance when invoking the pushed-back member-function pointer.
gg
Re: Polymorphic Boost Function