简体   繁体   English

函数指针和C ++模板

[英]Function pointers and C++ templates

I have a C++ code and i really need to use C function in it: 我有一个C ++代码,我真的需要在其中使用C函数:

int procedure(... , S_fp fun , ...)

where fun - is a pointer to function which signature must be like that: fun - 是一个指向函数的指针,签名必须是这样的:

int fun(double* , double* , double)

The function, pointer to which i want to use, is a member of a class template: 我想要使​​用的函数指针是类模板的成员:

template<int nPar> class PenaltyAlgorithm
{
public:
...
int Calculate(double* param, double* val, double prec)
{
   ...
}
...
}

As you can see, its signature is in order. 如您所见,它的签名是有序的。 But when I do this: procedure(... &PenaltyAlgorithm::Calculate, ...); 但是当我这样做时:procedure(...&PenaltyAlgorithm :: Calculate,...);

I get error: 我收到错误:

error: cannot convert ‘int (PenaltyAlgorithm<30>::*)(double*, double*, double)’ to ‘int (*)(...)’ for argument ...

Please, help. 请帮忙。 If you need any additional info, please, write. 如果您需要任何其他信息,请写信。

Thanks!!! 谢谢!!!

After reading answers, i understood that it's important to add information about class: 阅读答案后,我明白添加课程信息很重要:

template<int nPar> class PenaltyAlgorithm
{
public:
int Calculate(double* param, double* val, double prec)
    {
    *val = comp_fun->Compute(param);
    }

double* RunAlgorithm()
    {
        ...
        procedure(... &PenaltyAlgorithm<nPar>::Calculate, ...);
        ... 
    }
...
private:
...
CompositeFunction<nPar>* comp_fun;
}

1). 1)。 I can't use static fun, because this fun need to get access to members of class; 我不能使用静态的乐趣,因为这有趣的需要访问类的成员;

2). 2)。 Can we use the fact, that we call 'procedure' from fun-member of class? 我们可以使用这个事实,我们称之为“程序”来自有趣的成员吗?

Calculate is a non-static method on PenaltyAlgorithm. Calculate是PenaltyAlgorithm上的非静态方法。 It requires an instance of the PenaltyAlgorithm to execute, so it can't be fed to a C function as a plain function pointer. 它需要PenaltyAlgorithm的一个实例来执行,因此它不能作为普通函数指针提供给C函数。

Can the Calculate function be made static? 可以将Calculate函数设为静态吗? If it can, it should work the same as a C function pointer, only it won't be able to access non-static data in the PenaltyAlgorithm class. 如果可以,它应该与C函数指针一样工作,只有它不能访问PenaltyAlgorithm类中的非静态数据。

Remember that int Calculate(double* param, double* val, double prec) as an instance member has a hidden this pointer, that a global function or static member won't have. 请记住int Calculate(double* param, double* val, double prec)作为实例成员有一个隐藏的this指针,一个全局函数或静态成员将不具有。 Without this, it won't know which PenaltyAlgorithm to run it on. 没有它,它将不知道运行它的PenaltyAlgorithm

Either the pointer should be of type int (PenaltyAlgorithm::*)(double* param, double* val, double prec) or Calculate should be static. 指针应该是int (PenaltyAlgorithm::*)(double* param, double* val, double prec)类型int (PenaltyAlgorithm::*)(double* param, double* val, double prec)或者Calculate应该是静态的。

If int procedure(... , S_fp fun , ...) must definitely be a C function, then there is no way to do what you are looking for directly. 如果int procedure(... , S_fp fun , ...)绝对必须是C函数,那么就没有办法直接做你想要的了。 If you try to pass a class member function pointer it will have a hidden this pointer as its first argument in the stack, hence the prototypes won't match. 如果你试图传递一个类成员函数指针,它将有一个隐藏的this指针作为堆栈中的第一个参数,因此原型将不匹配。

However you can create a global or static function which internally uses a saved instance pointer to access the PenaltyAlgorithm class indirectly. 但是,您可以创建一个全局或静态函数,该函数在内部使用已保存的实例指针间接访问PenaltyAlgorithm类。 You can try the below: 您可以尝试以下方法:

1) Save the instance pointer of the class you want to pass to the procedure function to a global variable. 1)将要传递给procedure函数的类的实例指针保存到全局变量。

2) Create a new global function int CalculateGlobal(double* param, double* val, double prec) 2)创建一个新的全局函数int CalculateGlobal(double* param, double* val, double prec)

3) Pass the CalculateGlobal function's pointer as the function pointer to your procedure function. 3)将CalculateGlobal函数的指针作为函数指针传递给procedure函数。

4) CalculateGlobal function can then use the saved instance pointer to access the specific class' internals. 4)CalculateGlobal函数然后可以使用保存的实例指针来访问特定类的内部。

Well this is not a good way of doing things of course... 那么这当然不是一个很好的做事方式......

除非它是静态成员函数,否则不能将类似指针传递给成员函数。

You have to make Calculate static, use 你必须使Calculate静态,使用

static int Calculate(double* param, double* val, double prec)

instead of 代替

int Calculate(double* param, double* val, double prec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM