如何最好地将方法传递到同一类的方法中

How to best pass methods into methods of the same class

本文关键字:方法 一类 何最好      更新时间:2023-10-16

我有一个C++类,一个大型的复杂方法compute我想用"计算内核"(同一类的方法)馈送它。我想我会做一些类似的事情

class test {
int classVar_ = 42;
int compute_add(int a, int b)
{
   compute(int a, int b, this->add_())
}
int compute_mult(int a, int b)
{
   compute(int a, int b, this->mult_())
}

int compute_(int a, int b, "pass in add or multiply as f()")
{
   int c=0;
   // Some complex loops {
   c += f(a,b)
   // }
   return c;
}
int add_(int a, int b){a+b+classVar_;}
int multiply_(int a, int b){a*b+classVar_;}
...
}

但我不确定我将如何通过addmultiply.这种方法的另一种方法是传入某种ENUM来指定add()multiply(),但我想避免循环内的switchif

这里的最佳实践是什么?

正如您所怀疑的那样,传递成员函数指针是可以接受的做法。

如果您需要了解语法,它是:

int compute_(int a, int b, int (test::*f)(int,int))
{
   int c=0;
   // Some complex loops {
   c += (this->*f)(a,b)
   // }
   return c;
}

使用整数和切换来表示成员函数会引入程序员开销,以便在可用操作列表更改时使内容保持最新。所以你不希望这样,除非在特定情况下有一些重要的原因。

一种替代方法是使compute更加通用 - 而不是采用成员函数,编写一个采用任何可调用类型的函数模板:

template <typename BinaryFunction>
int compute_(int a, int b, BinaryFunction f) {
    // body as before but `f(a,b)` instead of `(this->*f)(a,b)`
}

如果有人想将其与自己发明的某些运算符一起使用,这个更通用的模板很棒,这不是test的成员函数。但是,在成员函数的情况下使用起来更困难,因为有人需要捕获this。有几种方法可以做到这一点 - C++11 lambda,boost::bind,或者写出一个函子。例如:

template <typename BinaryFunction>
int compute_(int a, int b, BinaryFunction f) {
    // body as before with `f(a,b)`
}
int compute_(int a, int b, int (test::*f)(int,int))
{
    return compute_(a, b, bind_this(f, this));
}

定义bind_this有点痛苦:它就像std::bind1st,只是我们想使用 3-arg 函子,而bind1st只接受二进制函子。 boost::bind 和 C++11 中的std::bind更灵活,并且可以处理额外的参数。以下内容适用于这种情况,但通常不适用于绑定 2-arg 成员函数:

struct bind_this {
    int (test::*f)(int,int);
    test *t;
    int operator(int a, int b) const {
        return (t->*f)(a,b);
    }
    bind_this(int (test::*f)(int,int), test *t) : f(f), t(t) {}
};

在 C++11 中,您可以只使用 lambda:

int compute_(int a, int b, int (test::*f)(int,int))
{
    return compute_(a, b, [=](int c, int d){ return (this->*f)(c,d) });
}

使用指向函数的指针。

int compute(int a, int b, int (test::*f) (int, int) )
{
   int c=0;
   // Some complex loops {
   c += (this->*f)(a,b)
   // }
   return c;
}

您有两种选择:

  1. 使用指向成员函数的指针
  2. 使用 lambda 函数

使用指向成员函数的指针的示例:

#include <iostream>
class D
{
public:
  D(int v ) : classVar_(v){}
  int add_(int a, int b){return (a+b+classVar_);}
  int multiply_(int a, int b){return (a*b+classVar_);}
private:
  int classVar_;
};
class test {
public:
int compute_(int a, int b, D &d, int (D::*f)(int a, int b))
{
   int c=0;
   // Some complex loops {
   c += (d.*f)(a,b);
   // }
   return c;
}
};
int main()
{
  test test;
  D d(1);
  std::cout<<"add : " << test.compute_( 5, 4, d, &D::add_ ) << std::endl;
  std::cout<<"add : " << test.compute_( 5, 4, d, &D::multiply_ ) << std::endl;
}

使用λ的示例:

#include <iostream>
#include <functional>
class D
{
public:
  D(int v ) : classVar_(v){}
  int add_(int a, int b){return (a+b+classVar_);}
  int multiply_(int a, int b){return (a*b+classVar_);}
private:
  int classVar_;
};
class test {
public:
int compute_(int a, int b, std::function< int(int,int) > f)
{
   int c=0;
   // Some complex loops {
   c += f(a,b);
   // }
   return c;
}
};
int main()
{
  test test;
  D d(1);
  std::cout<<"add : " << test.compute_( 5, 4, [&d](int a, int b){ return d.add_(a,b); } ) << std::endl;
  std::cout<<"add : " << test.compute_( 5, 4, [&d](int a, int b){ return d.multiply_(a,b); } ) << std::endl;
}