New binding operator?
I want to bind the new operator (see example below). If the constructor has no arguments it works fine, but if it has arguments I seem to have a problem with the binding syntax being correct.
#include <map>
#include <boost\function.hpp>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\construct.hpp>
#include <boost\lambda\bind.hpp>
enum TypeEnum
{
BarType,
BazType
};
class Foo
{
};
class Bar : public Foo
{
public:
Bar(int x)
{ BarVal = x; }
private:
int barVal;
};
class Baz : public Foo
{
public:
Baz(int x)
{ bazVal = 2 * x; }
private:
int bazVal;
};
class FooFactory
{
public:
FooFactory()
{
// How does this work?
factoryMap[BarType] = boost::lambda::bind(boost::lambda::new_ptr<Bar>(_1));
factoryMap[BazType] = boost::lambda::bind(boost::lambda::new_ptr<Baz>(_1));
}
Foo* getFoo(TypeEnum type, int z)
{
return factoryMap[type](z);
}
private:
std::map<TypeEnum, boost::function<Foo* (int)>> factoryMap;
};
int main()
{
FooFactory fooFactory;
Bar *newBar = static_cast<Bar*> (fooFactory.getFoo(BarType, 10));
return 0;
}
+2
source to share
2 answers