I look at a piece of Java code I would like to see how it would be implemented in C++:
public interface IThing {
public void method1();
// more (virtual methods)
}
public interface IThingFactory {
public IThing getThing(ThingType thing);
}
public interface IFactory<T> {
public T createInstance();
}
public class A {
A(ThingType thingType, IFactory<IThing> thingFactory, ...) {
// ...
}
static A create(ThingType thingType, final IThingFactory thingFactory) {
return new A(new IFactory<IThing>() {
{
public IThing createInstance()
{
return thingFactory.getThing(thingType);
}
}, new IFactory< ... >()
{
public IAnother createInstance()
{
return anotherFactory.getAnother(anotherType);
}
});
}
// ...
}
I hope above code (not complete) illustrates what I try to find out. My problem is
how that would be done in C++.
Mainly I do not understand the implementation of the createInstance
inside in A
constructor call (as it seems for me still incomplete), like an anonymous function implementation. I do not see a way how I could implement the createInstance
method in C++ in a way so that the object of (abstract) type IFactory<IThing>
is defined, because in this way the (virtual) method createInstance
is still pure. Or could that be done with some kind of lambda function?
Can somebody show me a way how this would be coded in C++? Thanks for the info!