我正在使用一个数值库 deal.ii,其中集成了许多数值工具。我发现奇怪的是我可以直接调用成员函数而无需定义对象。比如我可以直接调用
Vectortools::interpolate_boundary_condition();
Run Code Online (Sandbox Code Playgroud)
请问什么时候可以不定义对象直接调用成员函数?谢谢!
我正在研究优化算法,在我的算法中,我需要使用函数指针将函数传递给优化器
类定义:
class Downhill
{
public: //
Downhill(std::string fpath);
~Downhill();
void run();
private:
/*objfun will be passed to amoeba function by function pointer*/
float objfun1(float *p,int ndim);
float objfun2(float *p,int ndim);
float objfun3(float *p,int ndim);
void amoeba(float **p, float pval[], int ndim, float(*funk)(float *, int ndim), float ftol, int &niter);
private:
float ftol;
float TINY = 1E-10;
int NMAX = 5000;
std::ofstream fout;
};
Run Code Online (Sandbox Code Playgroud)
在成员函数 run() 的声明中,我做了如下操作:
float(*funk)(float *p, int dim);
funk = &(this->objfun1); // I define the function pointer …Run Code Online (Sandbox Code Playgroud)