-
June 30th, 2010, 04:15 AM
#1
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.
Thanks for your help.
-
June 30th, 2010, 01:57 PM
#2
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
-
July 2nd, 2010, 01:23 AM
#3
Re: Polymorphic Boost Function
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|