const函数指针 [英] const function pointers

查看:99
本文介绍了const函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用函数指针时遇到一些困难.我有一个基类,它定义了一个函数指针,该指针通过 typedef double(* function)(double * x)const;

I'm have some difficulties with function pointers. I have an base class which defines a function pointer that via typedef double (*function)(double *x) const;

  • 一个简单的问题:为什么上面的typedef不能编译?

  • A quick side question: why does the above typedef not compile?

给出以下错误:错误:"const"和"volatile"功能 类型声明中功能"的说明无效

Gives the following error: error: ‘const’ and ‘volatile’ function specifiers on ‘function’ invalid in type declaration

对于下面的部分,我使用 typedef double(* function)(double * x). 现在,每个子类都可以实现此功能的多个不同版本.通过一个枚举,我选择了自己选择的函数,该函数将我的非成员函数指针(在基类中定义)设置为由子类的这些成员函数指针之一初始化.这是一个代码段:

For the part below I use typedef double (*function)(double *x). Now each daughter class can implement multiple and different versions of functions of this type. Via an enum I select the function of my choice, which sets my non-member function pointer (defined in the base class) to be initialized by one of these member-function pointers of the daughter class. Here's a code snippet:

子类的源文件:

PndLmdROOTDataModel1D::PndLmdROOTDataModel1D(interpolation_type intpol_type) {
  if(intpol_type == CONSTANT) {
    setModelFunction(&PndLmdROOTDataModel1D::evaluateConstant); 
  }
  else if (intpol_type == SPLINE) {
    setModelFunction(&PndLmdROOTDataModel1D::evaluateSpline);
  }
  else {
    setModelFunction(&PndLmdROOTDataModel1D::evaluateLinear);
  }
}

和基类(头文件):

class MultiModel1D: public Model1D {
protected:
  function model_func;

public:
  MultiModel1D();
  virtual ~MultiModel1D();

  void setModelFunction(function f);
}

编译时出现以下错误:

注意:未知参数'double转换为参数1 (PndLmdROOTDataModel1D :: *)(double *)"改为"function {aka double (*)(double *)}’

note: no known conversion for argument 1 from ‘double (PndLmdROOTDataModel1D::*)(double*)’ to ‘function {aka double (*)(double*)}’

由于速度问题,我正在使用函数指针(至少我认为这应该比在某些开关情况下持续运行要快).我究竟做错了什么?也许还有一些设计模式可以作为更好的选择...预先感谢!

I'm using the function pointer, because of speed issues (at least I think this should be faster than constantly running through some switch case). What am I doing wrong? Maybe there is also some design pattern that will serve as a better alternative... Thanks in advance!

史蒂夫

推荐答案

这是因为(免费)函数指针与成员函数指针之间存在根本差异.您的附带问题"已经包含问题的提示.为了说明,您可以执行以下任一操作:

That is because there is a fundamental difference between a (free-)function pointer and a member function pointer. You "side question" already contains the hint to the problem. To explain, you can do either this:

typedef double (SomeClass::*function)(double *x) const;

typedef double (*function)(double *x);

,但永远不能在函数级别将非成员函数声明为const.这些类型不能相互转换,这就是编译器指出的代码中的问题.

but a non-member function can never be declared const on the function level. Those types can not be converted to each other and this is the problem in your code that the compiler is pointing out.

如果可以 进行转换,则会遇到一个问题:成员函数指针告诉编译器它需要一个对象来调用,该对象将在this时放入成员函数被调用.如果可以将指针强制转换为普通的函数指针,则该对象将丢失,结果可能会弄乱所有参数.所以,不,您真的不能只投下他们.现实更加复杂(多重/虚拟继承),但您了解了...

If they could be converted, you would end up with a problem: The member function pointer tells the compiler that it needs an object to be called with, which will be put into this when the member function is called. If the pointer could be casted to a normal function pointer, this object would be missing and probably all parameters would be messed up as a consequence. So, no, you really can't just cast them. The reality is even more complicated (multiple/virtual inheritance), but you get the picture...

这篇关于const函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆