function-like classes 仿函数笔记----C++学习之路
2017-11-07 22:09
288 查看
1.本次笔记只提如何实现仿函数。
2.函数是怎么样的呢,有函数名称,还有一个小括号,叫做函数调用操作符。如果有个东西能用到这个函数调用
操作符,那么就叫做他为函数,或者仿函数。
3.代码实例:
template<class T>
struct identity
{
const T& operator() ( const T& x) const { return x; }
};
template <class Pair>
struct select1st
{
const typename Pair::first_type& operator() (const Pair& x) const { return x.first; }
};
template <class Pair>
struct select2nd
{
const typename Pair::second_type& operator()(const Pair& x) const { return x.second; }
};看看Pair:
template <class T1, class T2>
struct pair
{
T1 first;
T2 second;
pair():first(T1()), second(T2()){}
pair(const T1& a,const T2& b)
:first(a),second(b) {}
.....
};这里的T1和T2都可以不一样。
再来看看这个function like,如果看到类的操作符重载operator()大部分都是为了让自己function like。
这种类做出来的对象叫做函数对象,又成为仿函数。
4.但是在3中的例子其实是不完整的代码,他们都继承了一个类unary_function。
以及在标准库中的plus也是仿函数,继承的是binary_function。
看下代码:
template <class Arg, class Result>
struct unary_function
{
typedef Arg argument_type;
typedef Result result_type;
};
template <class Arg1, class Arg2, class Result>
struct binary_function
{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};这两个类的大小都是为0.但是如果在代码上,用sizeof来找,它实际上却是1.
5.这次的笔记主要告诉自己,在标准库中有很多仿函数,这些仿函数都会继承一些奇怪的父类,这些
父类大小都为0.
2.函数是怎么样的呢,有函数名称,还有一个小括号,叫做函数调用操作符。如果有个东西能用到这个函数调用
操作符,那么就叫做他为函数,或者仿函数。
3.代码实例:
template<class T>
struct identity
{
const T& operator() ( const T& x) const { return x; }
};
template <class Pair>
struct select1st
{
const typename Pair::first_type& operator() (const Pair& x) const { return x.first; }
};
template <class Pair>
struct select2nd
{
const typename Pair::second_type& operator()(const Pair& x) const { return x.second; }
};看看Pair:
template <class T1, class T2>
struct pair
{
T1 first;
T2 second;
pair():first(T1()), second(T2()){}
pair(const T1& a,const T2& b)
:first(a),second(b) {}
.....
};这里的T1和T2都可以不一样。
再来看看这个function like,如果看到类的操作符重载operator()大部分都是为了让自己function like。
这种类做出来的对象叫做函数对象,又成为仿函数。
4.但是在3中的例子其实是不完整的代码,他们都继承了一个类unary_function。
以及在标准库中的plus也是仿函数,继承的是binary_function。
看下代码:
template <class Arg, class Result>
struct unary_function
{
typedef Arg argument_type;
typedef Result result_type;
};
template <class Arg1, class Arg2, class Result>
struct binary_function
{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};这两个类的大小都是为0.但是如果在代码上,用sizeof来找,它实际上却是1.
5.这次的笔记主要告诉自己,在标准库中有很多仿函数,这些仿函数都会继承一些奇怪的父类,这些
父类大小都为0.
相关文章推荐
- 虚函数 virtual function 笔记(与继承)----C++学习之路
- c++学习之路----笔记
- 深度探索C++对象模型学习笔记——Function语意学
- boolan——c++学习笔记之pointer-like classes
- template template parameter模板模板参数笔记----C++学习之路
- c++学习笔记——虚函数(virtual function)
- String类的设计笔记----C++学习之路
- c++ standard library 学习笔记-仿函数
- C++学习之路指针记录笔记(一)
- this pointer笔记----C++学习之路
- function template函数模板概念笔记----C++学习之路
- 重载new()delete()的笔记----C++学习之路
- c++ primer 学习之路 二 —— 实验楼作业笔记(1-3)
- member template成员模板笔记----C++学习之路
- 关于静态绑定与动态绑定的辨别笔记----C++学习之路
- reference引用笔记----C++学习之路
- C++学习笔记【第二部分第十二章:动态内存】
- 【C++学习笔记】枚举
- C++学习笔记(20161114 ) 根据指定目标时间获得时间戳(秒数)
- c++学习笔记:类的若干基础问题