Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:09:47+00:00 2026-05-24T23:09:47+00:00

If I have a polymorphic base class called Base as well as classes Derived1

  • 0

If I have a polymorphic base class called Base as well as classes Derived1 and Derived2 which inherit from Base. I can then use boost::lambda to create a factory of sorts. Something like:

typedef boost::function<Base *()> Creator;
std::map<std::string,Creator> map1;
map1["Derived1"] = boost::lambda::new_ptr<Derived1>();
map1["Derived2"] = boost::lambda::new_ptr<Derived2>();

(This isn’t real code, I’m just trying to illustrate the problem.)

This works, so I can then do a lookup in the map using a string and then invoke the lambda function to instantiate that class. All good.

The problem with this is that it’s dealing in raw pointers, I’d prefer to be using smart pointers (std::shared_ptr).

So if I change from:

typedef boost::function<Base *>() Creator;

to:

typedef boost::function<std::shared_ptr<Base> >() Creator;

Then I’m getting stuck from here. I’ve tried using boost::lambda::bind in conjunction with boost::lambda::new_ptr but I’m not having much luck, can’t get past compilation errors. (Huge reams of template-related error output.)

I’ve checked other similar messages within StackOverflow, Using boost::bind and boost::lambda::new_ptr to return a shared_ptr constructor is close but if I try to apply its solution I get the template errors mentioned above.

I’m happy to provide sample code and the actual errors if it helps, but hopefully the above info is sufficient. I’m using boost 1.47.0 on GCC 4.6 as well as 4.7 snapshot on Fedora 15.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:09:49+00:00Added an answer on May 24, 2026 at 11:09 pm
    class Base { 
    public:
        virtual ~Base() = 0;
    };
    Base::~Base() {}
    
    class Derived1 : public Base {};
    class Derived2 : public Base {};
    
    typedef boost::shared_ptr<Base> BasePtr;
    
    typedef boost::function<BasePtr()> Creator;
    template <typename T>
    Creator MakeFactory()
    {
        namespace la = boost::lambda;
        return la::bind( 
            la::constructor<BasePtr>(), 
            la::bind(la::new_ptr<T>()));
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        std::map<std::string,Creator> map1;    
        map1["Derived1"] = MakeFactory<Derived1>();
        map1["Derived2"] = MakeFactory<Derived2>();
        BasePtr p1 = map1["Derived1"]();
        BasePtr p2 = map1["Derived2"]();
    
        return 0;
    }
    

    however, why go to the trouble when you could write:

    template <typename T>
    BasePtr MakeFactoryImpl()
    {
        return BasePtr(new T());
    }
    template <typename T>
    Creator MakeFactory()
    {
        return Creator(&MakeFactoryImpl<T>);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have these classes: class Core < ActiveRecord::Base belongs_to :resource, :polymorphic => true
I have a declarative base class Entity which defines the column name as polymorphic,
I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from
Related: How can I use polymorphism in XML Serialization? I have a class I
I have two models, Article and Post that both inherit from a base model
I have a polymorhpic model as follows: class Upload < ActiveRecord::Base belongs_to :uploadable, :polymorphic
I have these classes: class Base { public: virtual void foo(int x = 0)
I have a base polymorphic class ( with virtual methods ) and a derived
I have this pimpl design where the implementation classes are polymorphic but the interfaces
Why can you not have a foreign key in a polymorphic association, such as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.