【问题标题】:put together a function pointer and a object [duplicate]将函数指针和对象放在一起[重复]
【发布时间】:2019-03-01 11:49:39
【问题描述】:

我尝试将成员函数 (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);

}

【问题讨论】:

    标签: c++


    【解决方案1】:

    差不多是:

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

    不可能

    • z.(*f) (std::forward&lt;Args&gt;(args)...))
    • z.(*(&amp;f)(std::forward&lt;Args&gt;(args)...));

    我们应该使用运算符.*(或-&gt;*)。

    如果f(args...) 将返回int test::*(成员指针),z.*f(std::forward&lt;Args&gt;(args)...) 将是有效的,因为它实际上是z .* (f(std::forward&lt;Args&gt;(args)...))

    【讨论】:

      猜你喜欢
      • 2012-08-04
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2011-02-12
      • 2019-08-10
      • 1970-01-01
      • 2018-08-24
      • 2011-02-09
      相关资源
      最近更新 更多