This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Anonymous class closures in g++


Hi everyone.

I'm interested in extending g++ to support Java-style anonymous classes, and I thought I would ping this list in case anyone here has any relevant advice, experience, etc..

For those who don't know what I'm talking about, here's an example:

[snippet]

class Callback {
public:
  virtual ~Callback() { }
  virtual void handle(int result);
};

class A {
public:
  void process(int a, int b);
};

void foo(Callback* c);

int bar(A* a, int x) {
  // call foo with an instance of an anonymous subclass of Callback:
  foo(new Callback() {
    void handle(int result) {
      a->process(result, x);
    }
  });
}

[/snippet]

Now, if bar were implemented in valid C++, we'd have this much more verbose and brittle version:

[snippet]

int bar(A* a, int x) {
  class ClosureCallback: public Callback {
    Action* a;
    int x;
  public:
    ClosureCallback(Action* a, int x): a(a), x(x) { }
    void handle(int result) {
      a->process(result, x);
    }
  };

  foo(new ClosureCallback(a, x));
}

[/snippet]

Anyway, I'm just now starting to look at the g++ code to find out where code supporting this feature might fit. If anyone else has any interest in this, please respond. Thanks.

- Joel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]