1

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!

Andreas W. Wylach
  • 723
  • 2
  • 10
  • 31

2 Answers2

1

The Java Language Spec writes:

Both unqualified and qualified class instance creation expressions may optionally end with a class body. Such a class instance creation expression declares an anonymous class (ยง15.9.5) and creates an instance of it.

That anonymous class is a subclass of the class named in the class instance creation expression (in your case: IFactory).

A special feature of anonymous classes is that they may access final variables of surounding scopes. This is implemented by providing their values to the constructor of the anonymous subclass, which then stores them in final fields in that subclass.

A direct translation to C++ would therefore be to instantiante a named subclass that takes these values as constructor parameters. Depending on what the code actually does, a more idiomatic translation might exist.

meriton
  • 68,356
  • 14
  • 108
  • 175
  • Thanks for the info on how this works in Java. I managed to implement the C++ version as a lambda function as described in `Does C++0x support Anonymous Inner Classes`. I just did not know I could get the solution with an lambda, cause I never used lambda's before. โ€“ Andreas W. Wylach Feb 18 '13 at 04:48
0

In C++ you can also have anonymous class, just as you can have anonymous struct (class is the same as struct with private).

However, I think you'd better not use this, because we can not explicitly make the anonymous class inherit from class B, in the main function it is casted to B* which is very ugly, what's more, new operator can not be directly applied to the anonymous class. Thus the anonymous instance c is allocated on stack, it will be deleted when the program runs out of scope, and will cause problem.

Using a named subclass is the standard way.

#include <iostream>
using namespace std;

// class B is a pure virtual class which is equivalent to interface in java.
class B
{
public:
    virtual int F() = 0;
};

class A
{
public:
    B * bb;
public:
    A(B* b);
};

A::A(B*b)
{
    bb = b;
}

int main()
{
    class{public: virtual int F(){return 10;}} c;
    A a((B*) &c);
    cout<<a.bb->F()<<endl;
}
Min Lin
  • 3,177
  • 2
  • 19
  • 32