将函数指针和对象放在一起

StackTourista

我尝试将成员函数 (func) 引用到函数 (Multiprotocol) 并使用此类的对象 (z) 调用它。但我的线路有问题

result = z.*f(std::forward<Args>(args)...));

我试过这样的事情

z.(*f) (std::forward<Args>(args)...)) 

z.(*(&f)(std::forward<Args>(args)...));

但我不知道如何做到这一点。有谁能够帮我?

class test
{ 
   public:
        static int func()
        {
            std::cout << "in func";
        }
 };


class MultiProtocol
{
   public:

      template<typename Function, typename... Args>
      bool functionImpl(Function f, Args&&... args)
      {
         int result = 0;
         result = z.*f(std::forward<Args>(args)...));
         return result;
      }

      private:
      test z;
};

主要是:

int main()
{
MultiProtocol rr;
rr.functionImpl(&test::func);

}
杰洛德42

差不多,就是:

(z.*f)(std::forward<Args>(args)...)

不可能是

  • z.(*f) (std::forward<Args>(args)...))
  • z.(*(&f)(std::forward<Args>(args)...));

因为我们应该使用运算符.*(或->*)。

z.*f(std::forward<Args>(args)...)如果f(args...)会返回int test::*(成员上的指针),则它实际上是有效的z .* (f(std::forward<Args>(args)...))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章