C++ exrtk - 可以在类中使用吗?

Ern*_*Mur 5 c++ nested class exprtk

我想从 exprtk 调用一个类的函数。( http://www.partow.net/programming/exprtk/ )

我想用symbol_table.add_function 用这个工具包注册一个函数。因此,需要从该工具包提供的 ifunction 派生我的类:

 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
      }
   };
Run Code Online (Sandbox Code Playgroud)

是否有可能以某种方式包含这个结构,一个类可以访问它,并且这个结构的 operator() 可以访问类中的数据?一种可能性是将该类的指针传递给结构的构造函数。有没有更好的办法?

Exp*_*per 0

状态/数据类可以拥有ifunction或作为引用传递(确保正确管理生命周期)或通过 a 传递std::shared_ptrifunction实例:

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_;
};
Run Code Online (Sandbox Code Playgroud)