允许函数指针代码处理成员函数 [英] Allowing function pointer code to work on a member function

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

问题描述




大家好


我有一个令我困惑的难题。


我在C中有一个很大的代码库,我尽可能快地转换为C ++

(也就是说,因为我不断学习新的习语和绊倒C ++

''features' )。 C代码的一部分是一些优化函数,

期望指向函数的指针,该函数发送一个双精度数组并返回

a double即简化..


void optimize(double(* funk)(double *));


我现在有一堆带有成员函数的类也需要加倍*

并返回我想在此优化器中使用的双倍,但编译器

不会让我因为:


错误:类型''double(namespace :: class ::)(double *)''的参数没有

匹配''double(*)(double *)''


好​​的 - 我没想过,但觉得必须有一个简单的答案。

但是,我尝试将这些函数声明为朋友(但

函数使用的类成员不再可用)和

向他们发送''指针''和BS BSPPL p.419:


typedef double(class :: * Pstd_mem)(怀疑le * pv);

Pstd_mem p =& class :: opt_ll;


...但到目前为止我没有尝试过任何工作而且我正在运行出于想法。可以

任何C ++专家都提出了一个解决方案(希望可靠,并且有效率高达b $ b)?


TIA并请CC在这里回复


Michael

_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /


_ / _ / _ / _ / _ / Hopkins Research Ltd

_ / _ / _ / _ /

_ / _ / _ / _ / _ / _ / _ / http://www.hopkins-research .com /

_ / _ / _ / _ /

_ / _ / _ / _ /''触摸未来''


_ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ / _ /


[见 http://www.gotw.ca/resources/clcm.htm for info about

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Hi all

I have a conundrum that is puzzling me.

I have a large codebase in C that I am converting to C++ as fast as possible
(i.e. slowly because I keep learning new idioms and stumbling with C++
''features''). One part of the C code is some optimisation functions that
expect a pointer to a function which is sent an array of doubles and returns
a double i.e. simplified..

void optimise( double (*funk)( double* ) );

I now have a bunch of classes with member functions which also take double*
and return double that I want to use in this optimiser, but the compiler
won''t let me because:

error: argument of type ''double (namespace::class::)(double*)'' does not
match ''double (*)(double*)''

OK - I hadn''t thought about that but felt there must be an easy answer.
However, I have tried declaring the functions in question as friends (but
the class members that the functions use are no longer available) and
sending ''pointers'' to them a la BS TCPPPL p.419:

typedef double (class::*Pstd_mem)( double* pv );
Pstd_mem p = &class::opt_ll;

...but nothing I have tried so far works and I am running out of ideas. Can
any C++ gurus out there suggest a solution (that is hopefully reliable and
efficient)?

TIA and please CC replies here

Michael
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

_/ _/ _/_/_/ Hopkins Research Ltd
_/ _/ _/ _/
_/_/_/_/ _/_/_/ http://www.hopkins-research.com/
_/ _/ _/ _/
_/ _/ _/ _/ ''touch the future''

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

查询,Hopkins Research写道:
Enquiries, Hopkins Research wrote:
我在C中有一个很大的代码库,我是尽可能快地转换为C ++(即缓慢因为我不断学习新的习语和使用C ++''功能''的绊脚石)。 C代码的一部分是一些优化函数,它们期望指向一个函数的指针,该函数发送一个双精度数组并返回一个双精度函数。

void优化(double(* funk)(double *));

我现在有一堆带有成员函数的类,它们也会使用
double *并返回我希望在此优化器中使用的double ,但是
编译器不会让我因为:

错误:类型''double(namespace :: class ::)(double *)''的参数
不匹配''double(*)(double *)''


它们是不兼容的。


请参阅有关成员指针的常见问题解答。您可以在此处找到常见问题解答:
http:// www。 parashift.com/c++-faq-lite/

[..]
I have a large codebase in C that I am converting to C++ as fast as
possible (i.e. slowly because I keep learning new idioms and
stumbling with C++ ''features''). One part of the C code is some
optimisation functions that expect a pointer to a function which is
sent an array of doubles and returns a double i.e. simplified..

void optimise( double (*funk)( double* ) );

I now have a bunch of classes with member functions which also take
double* and return double that I want to use in this optimiser, but
the compiler won''t let me because:

error: argument of type ''double (namespace::class::)(double*)'' does
not match ''double (*)(double*)''
They are incompatible.

See FAQ about pointers to members. You can find the FAQ here:
http://www.parashift.com/c++-faq-lite/
[..]




如果你有疑问阅读常见问题,请问他们。


V



If you''ll have questions after reading the FAQ, do ask them.

V


查询,Hopkins Research写道:
Enquiries, Hopkins Research wrote:
大家好

我有一个令我困惑的难题。

我在C中有一个很大的代码库,我正在尽快转换为C ++
(即慢慢地因为我不断学习新的习语和绊倒C ++
''features'')。 C代码的一部分是一些优化函数,它们希望指向一个函数的指针,该函数被发送一个双精度数组并返回一个双精度函数,即简化...

void优化(double(* funk)(double *));

我现在有一堆带有成员函数的类,它们也需要使用double *
并返回我想在此优化器中使用的double ,但编译器不会让我因为:

错误:类型''double(namespace :: class ::)(double *)''的参数不是
匹配''double(*)(double *)''

好的 - 我没想过,但觉得必须有一个简单的答案。
但是,我有尝试将这些函数声明为朋友(但是函数使用的类成员不再可用)和
向他们发送''指针'到la BS TCPPPL p.419:

typedef double(class :: * Pstd_mem)(double * pv);
Pstd_mem p =& class :: opt_ll;

..但到目前为止我没有尝试过任何工作,而且我的想法已经不多了。那些任何C ++专家都可以建议一个解决方案(希望可靠且高效)?
Hi all

I have a conundrum that is puzzling me.

I have a large codebase in C that I am converting to C++ as fast as possible
(i.e. slowly because I keep learning new idioms and stumbling with C++
''features''). One part of the C code is some optimisation functions that
expect a pointer to a function which is sent an array of doubles and returns
a double i.e. simplified..

void optimise( double (*funk)( double* ) );

I now have a bunch of classes with member functions which also take double*
and return double that I want to use in this optimiser, but the compiler
won''t let me because:

error: argument of type ''double (namespace::class::)(double*)'' does not
match ''double (*)(double*)''

OK - I hadn''t thought about that but felt there must be an easy answer.
However, I have tried declaring the functions in question as friends (but
the class members that the functions use are no longer available) and
sending ''pointers'' to them a la BS TCPPPL p.419:

typedef double (class::*Pstd_mem)( double* pv );
Pstd_mem p = &class::opt_ll;

..but nothing I have tried so far works and I am running out of ideas. Can
any C++ gurus out there suggest a solution (that is hopefully reliable and
efficient)?




自由函数和成员函数是不同的。请参阅
http:// www .parashift.com / c ++ - faq-lit ... o-members.html

特别是33.2。当你在那里,阅读整个常见问题,它可能会帮助你。
Jonathan

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Free functions and member functions are different. See
http://www.parashift.com/c++-faq-lit...o-members.html,
especially 33.2. While you''re there, read the whole faq, it may help
you.
Jonathan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


2005年10月22日星期六13:48:30 -0400,Victor Bazarov < v。******** @ comAcast.net>

写道:
On Sat, 22 Oct 2005 13:48:30 -0400, "Victor Bazarov" <v.********@comAcast.net>
wrote:
我现在有一堆带有成员函数的类,它们也需要使用
double *并返回我想在此优化器中使用的double,但编译器不会让我因为:
error:类型''double(namespace :: class ::)(double *)''的参数确实
不匹配''double(*)(double *)''
I now have a bunch of classes with member functions which also take
double* and return double that I want to use in this optimiser, but
the compiler won''t let me because:

error: argument of type ''double (namespace::class::)(double*)'' does
not match ''double (*)(double*)''



它们是不兼容的。



They are incompatible.




但你可以用它们指向_static_成员函数,这可能会令人满意

OP'的目标。


-dr



But you can use them to point to _static_ member functions, which may satisify
the OP''s goal.

-dr


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

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