<meta content="text/html; charset=utf-8" http-equiv="CONTENT-TYPE">
<meta content="OpenOffice.org 2.2 (Linux)" name="GENERATOR">
<meta content="freebird" name="AUTHOR">
<meta content="20070510;9550000" name="CREATED">
<meta content="root" name="CHANGEDBY">
<meta content="20070717;14010300" name="CHANGED">
<style type="text/css">
<!--
@page { size: 8.5in 11in; margin: 0.79in }
H1 { margin-top: 0.24in; margin-bottom: 0.23in; line-height: 200%; page-break-inside: avoid }
H1.western { font-family: "Liberation Serif", serif; font-size: 22pt }
H1.cjk { font-family: "DejaVu Sans"; font-size: 22pt }
H1.ctl { font-family: "DejaVu Sans"; font-size: 22pt }
P { margin-bottom: 0.08in }
-->
</style>
Function Objects
函数对象的定义:可以使用函数调用语法进行调用的任何对象。
如果一个类或者结构A实现了ResultType operator()(Parameters)方法(即重载了函数调用操作符),当客户编写代码如A()时,其实就是通过默认构造函数创建了A的对象,然后调用上面的这个operator()方法。
函数对象拥有几个优点:
1)因为函数对象可以有成员变量,因此可以在构造函数中初始化成员变量,因此与一般函数相比,函数对象拥有状态。这主要带来的是灵活性。
比如函数Func接受一个函数对象的引用A,然后调用A(),客户可以预先创建不同的函数对象,比如A(1),A(2)(假设A类构造函数接受int参数),然后依次交给Func函数回调。如果只是一个普通的函数,因为没有状态是做不到这么灵活的回调的。
2)函数对象是一个类,如果将函数对象作为一个模板参数,可以指定某种行为,如果是传递给容器类的话,容器类也会因为模板参数的不同而不同。而函数指针无法做到这点。
关于这点,可以参考<<C++ Templates 中文版>>22.4.2节。
3)函数对象的性能比函数指针高。
函数调用有直接调用,间接调用和内联调用。内联调用概念不用再说,性能很高。直接调用的意思是指令中已经包含了函数的入口地址,而间接调用通常还需要在运行时才能确定调用拿一个函数。如果通过函数名调用,就是直接调用,如果通过函数指针调用,通常会导致间接调用。函数对象通常会产生直接调用或者内联调用,因此性能比函数指针高。
分享到:
相关推荐
泛型算法与Function Objects,繁体中文版,介绍C++开发过程中,STL与TyptTraits等技术,值得一看。
泛型演算法Generic Algorithms與Function Objects.zip
a matlab function which read 3d objects in vrlm format
JavaScript Objects Functions and Arrays Explained covers a lot of material as you may see from the index below. However, each chapter is self-contained and easy to understand with plenty of ...
远程对象 分布式对等网络中的远程对象。 地位 实施的: ... 通过代理调用本地和远程对象。 浏览器和node.js均受支持。... 对等网络,当前使用中央代理... listen ( PORT , function ( ) { console . log ( 'proxy server
However, to wrap subclassing and hooking into virtual function overriding it is an obscure technique unknown to a large population of Windows programmers. This is unfortunate, because there are many ...
The function templates that define algorithms are explained in detail, and you'll learn how to pass function objects or lambda expressions to them to customize their behavior. Many working examples ...
Sure, the STL has iterators, algorithms, and function objects, but for most C++ programmers, it's the containers that stand out. More powerful and flexible than arrays, they grow (and often shrink) ...
evaluation—often encapsulated in function definitions. It de-emphasizes or avoids the complexity of state change and mutable objects. This tends to create programs that are more succinct and ...
He offers detailed coverage of higher-order function objects that enable you to write code that is more concise, expressive, and readable. He even takes you "behind the scenes" with Boost, revealing ...
第1章STL概论与版本简介 ...第7章 仿函数(functors,另名 函数对象function objects) 第8章 配接器(adapters) 附录A 参考书籍与推荐读物 附录B 候捷网站(本书支持站点简介) 附录C STLPort 的移植经验(by孟岩)
The C++ Standard Library: A Tutorial and Reference, Second... The book focuses in particular on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms.
用radial basis function 来做骨骼提取的方法
The C++ Standard Library provides a set of common classes... In particular, the text focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms.
第6章 算法(algorithms) 第7章 仿函数(functors,另名 函数对象function objects) 第8章 配接器(adapters) 附录A 参考书籍与推荐读物 附录B 候捷网站(本书支持站点简介) 附录C STLPort 的移植经验(by孟岩)...
C++ Recipes: A Problem-Solution Approach is a handy code cookbook reference guide that cover the... A wealth of STL templates on function objects, adapters, allocators, and extensions are also available.
You’ll discover dozens of examples, diagrams, and illustrations that break down the functional concepts you can apply in C++, including lazy evaluation, function objects and invokables, algebraic ...
we collect tracklets that are assigned to the same category from all videos, and co-select tracklets that belong to true objects by solving a submodular function. This function accounts for object ...
How to leverage the wealth of C++ templates found in the STL, including function objects, adapters, allocators and more How to extend the STL and more Who this book is for This book is for those with ...
C++ Reference General Topics * FAQ * Pre-processor commands * Operator Precedence * Escape Sequences * ASCII Chart * Data Types ... Function objects/adapters * General utilities