CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    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

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Polymorphic Boost Function

    Thanks codeplug.
    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
  •  





Click Here to Expand Forum to Full Width

Featured