如何最好地将方法传递到同一类的方法中
How to best pass methods into methods of the same class
我有一个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_;}
...
}
但我不确定我将如何通过add
或multiply
.这种方法的另一种方法是传入某种ENUM
来指定add()
或multiply()
,但我想避免循环内的switch
或if
。
这里的最佳实践是什么?
正如您所怀疑的那样,传递成员函数指针是可以接受的做法。
如果您需要了解语法,它是:
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;
}
您有两种选择:
- 使用指向成员函数的指针
- 使用 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;
}
相关文章:
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 使用同一类中的方法重新分配字段时出现问题
- 有没有办法从同一类中的函子调用类方法?
- 从同一类中的另一个方法调用方法时出错
- C++采用同一类实例的类方法
- 如何将一个方法中的值返回到同一类的另一个方法
- 如何将数据从公共方法添加到同一类中的私有成员数组中?
- 什么可能导致方法不<<同一类中的字符串?
- Qt时隙方法(这是另一类)在发射信号时不触发
- 为什么pthread_create不在同一类中采用我的方法
- 有什么方法可以测试来自同一类的两个作业对象吗
- 将同一类的方法实现放在不同的对象文件中
- 一个类应使用功能指针调用另一类的方法
- 在模板类中:使用类方法作为另一类方法的模板参数
- 如何使一个方法在新线程中调用同一类中的另一个方法
- 方法无法访问同一类的成员变量 (C++)
- 同一类的不同方法中的指针向量
- 同一类中的虚拟方法与非虚拟方法
- 来自同一类的C++方法调用
- 向量中另一类的重音方法