使用Boost Factory在构造函数中传递参数 [英] Passing arguments in constructor with boost factory

查看:144
本文介绍了使用Boost Factory在构造函数中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对整个工厂的实施还很陌生,结果我的问题听起来可能是错误的,而且定义不明确。
因此,我想简单地说一下,以便初始化派生类的分支,到目前为止,我已经设法对具有空构造函数的类进行了初始化。现在让我介绍一下我的当前boost工厂实现的两个小类:

I am fairly new with the whole factory implementation and as a result maybe my question will sound wrong and not very well defined. So, with few words I want to have a boost factoty in order to initialize a banch fo derived classes and so far I have managed to do so for classes with empty constructors. Let me present to my current boost factory implementation for two small classes:

Base.h:

#ifndef BASE_H_
#define BASE_H_

#include <vector>
#include <map>
#include "boost/function.hpp"

class base {

protected:
    typedef boost::function <base *()> basefactory;

public:
      base();
      virtual ~base();

     int a;

     static std::map<std::string,base::basefactory>& b_factory();

  };

#endif /* BASE_H_ */

Base.cpp :

#include "base.h"

base::base() {
   // TODO Auto-generated constructor stub
}

base::~base() {
  // TODO Auto-generated destructor stub
}

static std::map<std::string,base::basefactory>& base::b_factory()
{
  static std::map<std::string,base::basefactory>* ans =
  new std::map<std::string,base::basefactory>();
  return *ans;
}

Derived.h:

#ifndef DERIVED_H_
#define DERIVED_H_

#include "boost/function.hpp"
#include "boost/functional/factory.hpp"
#include <iostream>

#include "base.h"

class derived : public base {
     public:
         derived();
         virtual ~derived();

          int b;

          static class _init {
            public:
               _init() {
                       base::b_factory()["derived"] = boost::factory<derived*>();
                       }
           }_initializer;

 };

 #endif /* DERIVED_H_ */

Derived.cpp :

#include "derived.h"

derived::derived() {
    // TODO Auto-generated constructor stub
}

derived::~derived() {
    // TODO Auto-generated destructor stub
}

derived::_init derived::_initializer;

因此,上面显示的代码对于类的空构造函数很好用,但是我不确定在基类和派生类构造函数需要接收参数的情况下,如何修改代码。更具体地说,假设我想拥有基本构造函数:

So, the above desplayed code works just fine for empty constructor of the classes, but I am very unsure how to modify the code in case the base and derived class constructors need to receive arguments. More specifically let's say that I want to have the base constructor:

base(int alpha) { 
a = alpha;
}

还有派生的构造函数:

derived(int alpha, int beta) : base( alpha ) // in order to pass the argument alpha to the base class
{ 
    b = beta;
}

因此,如上所述,我真的不确定我需要进行哪些修改为了使上述boost工厂实现适用于我的代码。
我知道在线上其他地方都有一些针对参数化构造函数的帖子,但他们并没有设法让我自己正确地理解如何做,这就是为什么我将其发布给她。
任何帮助/建议都将不胜感激!

So, as said above I am really unsure what modifications I need to do in order to manage to have the above boost factory implementation working for my code. I know there are some posts in here in elsewhere online for parametrized constructors but they didn't manage to make me properly understand how to do it myself and that's why I made this post her. Any kind of help/suggestion would be much appreciated!

推荐答案

如果您想要一个带有2个参数的工厂,您可以这样做:

If you want a factory which takes 2 arguments, you may do:

std::map<std::string, boost::function<base* (int, int)>> factories;
factories["derived"] = boost::bind(boost::factory<derived*>(), _1, _2);

std::unique_ptr<base> b{factories.at("derived")(42, 52)};

如果要修正参数,可以这样做

If you want to fix arguments, you may do

std::map<std::string, boost::function<base* ()>> factories;
factories["derived"] = boost::bind(boost::factory<derived*>(), 42, 52);
std::unique_ptr<base> b{factories.at("derived")()};

演示

这篇关于使用Boost Factory在构造函数中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆