传递一个对象及其方法之一作为参数
pass an object and one of its methods as an argument
我有一个包含一些算法的模型,我必须以不同的方式多次测试这些算法。 我很难 更改 class 中的任何内容以进行测试(在这么多文件中)。我想告诉编译器 运行 哪个对象上的哪个方法。每次,我都有两种算法进行比较,我有超过 10 个测试文件test1.cpp
... test10.cpp
...。因此很难调整每个测试文件。每个文件中算法的名称也不同。我正在寻找一种将方法从 main
传递给 profiler
的方法。事实上,一切都只能从 main 进行调整。我只将算法 copy/past 放入模型 class 中,然后修复主要函数而不更改 class 中的任何内容(在 copy/past 算法之后)或配置文件函数。下面的代码显示了我所需要的。在不将模型 class 分成两个 class 的情况下,随意调整此代码的结构。我必须只留一个class.
请不要将此问题发送(迁移)到代码审查,因为它是代码草案(上次我因为某人的错误而得到这么多反对票。)
欢迎提出最简单易读的建议。
#include <iostream>
class CModel
{
public:
// ....
// ....
// ....
CModel()
{
}
double algorithm1()
{
double result=0;
// ...
return result;
}
double algorithm2()
{
double result=0;
// ...
return result;
}
};
void profiler(CModel &model,double (*algorithm)(void))
{
// CTimer mytimer;
// mytimer.start();
// using model fields here
double result=model.(*algorithm)();
// mytimer.stop();
std::cout<<"out: "<<result<<std::endl;
// std::cout<<"time elapsed: "<<mytimer.duration;
}
int main()
{
CModel m1, m2;
// m1.something= something else;
// m2.something= something else;
profiler(m1,m1.algorithm1); // *** impossible ***
profiler(m2,m2.algorithm2); // *** impossible ***
return 0;
}
需要声明函数才能带成员函数
void profiler(CModel &model, double (CModel::*algorithm)())
你需要给函数传递一个指向成员函数的指针
profiler(m1, &CModel::algorithm1);
profiler(m2, &CModel::algorithm2);
你需要正确调用成员函数
double result = (model.*algorithm)();
有关详细信息,请参阅 C++, function pointer to member function。
以这种方式更改分析器功能怎么样?
#include <functional>
void profiler(std::function<void()> func) {
// CTimer mytimer;
// mytimer.start();
func();
// mytimer.stop();
// std::cout<<"time elapsed: "<<mytimer.duration;
}
使用分析器:
CModel m1, m2;
profiler([&m1](){
double d = m1.algorithm1();
});
profiler([&m2](){
double d = m2.algorithm2();
});
或者如果可能的话只是同一个对象:
CModel m1;
profiler([&m1](){
double d = m1.algorithm1();
});
profiler([&m1](){
double d = m1.algorithm2();
});
我有一个包含一些算法的模型,我必须以不同的方式多次测试这些算法。 我很难 更改 class 中的任何内容以进行测试(在这么多文件中)。我想告诉编译器 运行 哪个对象上的哪个方法。每次,我都有两种算法进行比较,我有超过 10 个测试文件test1.cpp
... test10.cpp
...。因此很难调整每个测试文件。每个文件中算法的名称也不同。我正在寻找一种将方法从 main
传递给 profiler
的方法。事实上,一切都只能从 main 进行调整。我只将算法 copy/past 放入模型 class 中,然后修复主要函数而不更改 class 中的任何内容(在 copy/past 算法之后)或配置文件函数。下面的代码显示了我所需要的。在不将模型 class 分成两个 class 的情况下,随意调整此代码的结构。我必须只留一个class.
请不要将此问题发送(迁移)到代码审查,因为它是代码草案(上次我因为某人的错误而得到这么多反对票。)
欢迎提出最简单易读的建议。
#include <iostream>
class CModel
{
public:
// ....
// ....
// ....
CModel()
{
}
double algorithm1()
{
double result=0;
// ...
return result;
}
double algorithm2()
{
double result=0;
// ...
return result;
}
};
void profiler(CModel &model,double (*algorithm)(void))
{
// CTimer mytimer;
// mytimer.start();
// using model fields here
double result=model.(*algorithm)();
// mytimer.stop();
std::cout<<"out: "<<result<<std::endl;
// std::cout<<"time elapsed: "<<mytimer.duration;
}
int main()
{
CModel m1, m2;
// m1.something= something else;
// m2.something= something else;
profiler(m1,m1.algorithm1); // *** impossible ***
profiler(m2,m2.algorithm2); // *** impossible ***
return 0;
}
需要声明函数才能带成员函数
void profiler(CModel &model, double (CModel::*algorithm)())
你需要给函数传递一个指向成员函数的指针
profiler(m1, &CModel::algorithm1); profiler(m2, &CModel::algorithm2);
你需要正确调用成员函数
double result = (model.*algorithm)();
有关详细信息,请参阅 C++, function pointer to member function。
以这种方式更改分析器功能怎么样?
#include <functional>
void profiler(std::function<void()> func) {
// CTimer mytimer;
// mytimer.start();
func();
// mytimer.stop();
// std::cout<<"time elapsed: "<<mytimer.duration;
}
使用分析器:
CModel m1, m2;
profiler([&m1](){
double d = m1.algorithm1();
});
profiler([&m2](){
double d = m2.algorithm2();
});
或者如果可能的话只是同一个对象:
CModel m1;
profiler([&m1](){
double d = m1.algorithm1();
});
profiler([&m1](){
double d = m1.algorithm2();
});