← Back to team overview

dolfin team mailing list archive

Anonymous classes in Function definitions

 

I just realized that there is a nice way to simplify the definition of
Functions and BoundaryConditions. The typical way to create a Function
(and similarly for BoundaryCondition) is to make a subclass of
Function and then create an object of that class:

  class MyFunction : public Function
  {
    real eval(const Point& p, unsigned int i)
    {
      return p.x * sin(p.y);
    }
  };

  MyFunction f;

A simpler way to do this is to create an anonymous class and directly
initialize the Function. That way, you save a line of code and you
don't have to come up with a name for the class:

  class : public Function
  {
    real eval(const Point& p, unsigned int i)
    {
      return p.x * sin(p.y);
    }
  } f;

Also note that you can do this inside functions and other classes, so
it's straightforward to create a new Function anywhere in the
code. See new convection-diffusion demo for an illustration.

Nice.

/Anders



Follow ups