5

I want to call a function of a class from exprtk. (http://www.partow.net/programming/exprtk/)

I want to register a function with this toolkit with symbol_table.add_function. Therefore it is required to derive my class like this from ifunction provided with that toolkit:

 template <typename T>
   struct foo : public exprtk::ifunction<T>
   {
      foo() : exprtk::ifunction<T>(0)
      {}

      T operator()()
      {
         // here I want to access data from a class which owns this struct
      }
   };

Is it possible to include this struct in a way, that a class can access it and the operator() of this struct can access data in the class? One possibility would be to pass a pointer of that class to the constructor of the struct. Is there a better way?

Ernie Mur
  • 350
  • 1
  • 15
  • A pointer would be one option. Another option could be to pass a reference instead. A third option could be to use multiple inheritance i.e. deriving `foo` from `exprtk::ifunction<>` _and_ your other class. Without more context it is hard to say which one is the best (or most appropriate). This said, some ad.: If you don't need the complete power of exprtk, you may consider to build your own expression parser. This might be a start: [SO: The Tiny Calculator Project](https://stackoverflow.com/a/46965151/7478597). – Scheff's Cat Dec 06 '17 at 11:53
  • So it looks like I have to pass a pointer/reference. Multiple inheritance is not an option, because I need several such functions and each time operator() is called from exprtk. – Ernie Mur Dec 06 '17 at 13:45
  • "Multiple inheritance is not an option, because I need several such functions and each time operator() is called from exprtk." Multiple inheritance doesn't prevent overloading of `operator()()` or usage in exprtk. But if instances of multiple classes (inherited from exprtk::ifunction) have to share the data of the same instance of your class _then_ multi inheritance is, of course, not an option. In that case, pointer or reference... – Scheff's Cat Dec 06 '17 at 14:00

0 Answers0