C++ exprtk - can it be used within a class?

440 views Asked by At

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?

1

There are 1 answers

1
ExprTk Developer On

The state/data class can either own the ifunction or be passed in as a reference (be sure to manage life-times properly) or via a std::shared_ptr to the ifunction instance:

class my_class
{};

template <typename T>
struct foo : public exprtk::ifunction<T>
{
   foo(my_class& mc)
   : exprtk::ifunction<T>(0)
   , mc_(mc)
   {}

   T operator()()
   {
      return mc_.get_some_value() / 2;
   }

   my_class mc_;
};