函数对象与函数指针 [英] Function objects vs. function pointers

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

问题描述

对于诸如回调,从列表中选择功能

以及其他东西,函数对象或函数指针这样的东西更有效?

谢谢!!!

Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?
Thanks!!!

推荐答案

* Protoman:
* Protoman:
对于像回调,选择功能的东西更有效
从列表和其他东西,函数对象或函数指针?
谢谢!!!
Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?
Thanks!!!




红色的,显然是在561行。 br />

如果你对一个认真的答案感兴趣,那么在你问之前你必须自己做这件事吗?b b b b b b b b b b b b b要点:


*以可衡量的方式定义效率(例如运行时间,

内存消耗,开发时间,开发成本......)。


*定义你感兴趣的案例。


*定义你感兴趣的平台。


*创建和/ o确定测试程序。


*测量。


其他任何事情都是关于假设案例的纯粹推测。


但首先,请记住(或在你的耳后)


过早优化是所有邪恶的根源。


优化总是一种权衡,这意味着你可以交易一些东西,

,通常是可维护性和正确性。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?



The red one, obviously, at line 561.

If you''re interested in a serious answer you''ll have to do the work
yourself before asking, so then there''s not much point in asking:

* Define efficiency in a measurable way (e.g. running time,
memory consumption, development time, development cost, ...).

* Define the cases you''re interested in.

* Define the platforms you''re interested in.

* Create and/or identify test programs.

* Measure.

Anything else is pure speculation about hypothetical cases.

But first of all, keep at heart (or behind your ear) that

Premature optimization is the root of all evil.

Optimization is always a trade-off, meaning you trade away something,
and usually that something is maintainability and correctness.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


为了提高效率,我的意思是内存和速度,例如,我的意思是回调

并从列表中选择,平台我指的是工作站和服务器。

这是一个测试程序:


#include< iostream>

#include< cstdlib>

使用namespace std;


类Functor

{

public:

void operator()() const {cout<< "你好!" << endl;}

};


void(* pfn)();


void fn(){ cout<< "你好!" <<
void call(void(* pfn)()){pfn();}

void callback(const Functor& fn){fn();}


int main()

{

pfn = fn;

call(pfn);

callback(Functor());

system(" PAUSE");

返回0;

}


有什么建议吗?谢谢!!!

For efficiency , I mean memory and speed, for cases, I mean callbacks
and selecting from a list, platforms I mean workstations and servers.
Here''s a test program:

#include <iostream>
#include <cstdlib>
using namespace std;

class Functor
{
public:
void operator()()const{cout << "Hello!!!" << endl;}
};

void(*pfn)();

void fn(){cout << "Hello!!!" << endl;}

void call(void(*pfn)()){pfn();}

void callback(const Functor& fn){fn();}

int main()
{
pfn=fn;
call(pfn);
callback(Functor());
system("PAUSE");
return 0;
}

Any suggestions? Thanks!!!


嗯,我只是在这里推测,但如果你是用C ++写的,为什么要用

a C风格的回调机制?

你可以定义一个接口类,将回调作为纯虚拟

成员函数,让其他人在派生类中实现它们。 br />
你不需要这样的函数指针和函子。我不确定

关于它的效率,但代码将以

的方式更有意义。


Yuval。

Well, I''m just speculating here, but if you are writing in C++, why use
a C-style callback mechanism?
You can define an interface class, with the callbacks as pure virtual
member functions, and let the others implement them in derived classes.
You won''t need function pointers nor functors in that way. I''m not sure
about its efficiency, but the code will make much more sense in that
way.

Yuval.


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

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