C++中的函數指針和函數對象

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

//c++ function object
// class Add  
// {  
// public:  
//  int operator()(int a, int b)  
//  {  
//      return a + b;  
//  }  
// };  

// int main(int argc, char *argv[])
// {
//  Add add; // 定義函數對象  
//  cout << add(3,2); // 5 
//  return 0;
// }


//function pinter
// int AddTunc(int a, int b)
// {
//  return a+b;
// }

// typedef int (*FunctionPointer)(int a, int b);
// //or like this, typedef int (*FunctionPointer)(int, int );


// int main(int argc, char* argv[])
// {
//  FunctionPointer add = &AddTunc;
//  //or like this , FunctionPointer add = AddTunc;
//  cout<<add(2,8);
// }

//  //function object with additional data
// class IsLess
// {
// public: 
//  IsLess(int a) : value(a)
//  {}

//  bool operator()(int value)
//  {
//      return this->value < value;
//      //return value < value1;
//  }
// private:
//  int value;  
// };

// int main(int argc, char* argv[])
// {
//  IsLess less(10);
//  cout<<less(9)<<" "<<less(11)<<"\n";

//  const int SIZE = 5;  
//  int array[SIZE] = { 50, 30, 9, 7, 60};  
//  // 找到小於數組array中小於10的第一個數的位置  
//  int * pa = find_if(array, array + SIZE, IsLess(50)); 
//  // pa point to the number less bigger than 50, or pa pointe to the end
//  if(array + SIZE != pa )
//      cout<<"*pa= "<<*pa<<"\n";  

//  pa = find_if(array, array + SIZE, IsLess(60)); 
//  // pa point to the number less bigger than 60, or pa pointe to the end
//  if(array + SIZE != pa )
//      cout<<"*pa= "<<*pa<<"\n";  
// }



//function: count the number  according  to  a special condition 
// template<typename FUNC> 
// int count_n(int* array, int size, FUNC func)  
// {  
//  int count = 0;  
//  for(int i = 0; i < size; ++i){
//      if(func(array[i]))  
//          count ++;       
//  }  

//  return count;  
// } 

// class IsLess
// {
// public: 
//  IsLess(int a) : value(a)
//  {}

//  bool operator()(int value)
//  {
//      return this->value < value;
//      //return value < value1;
//  }
// private:
//  int value;  
// };

// bool less10(int value)  
// {  
//  return 10 < value;  
// }  
// int main()
// {
//  const int SIZE = 5;  
//  int array[SIZE] = { 50, 30, 9, 7, 20};  
//  cout << count_n(array, SIZE, IsLess(10))<<"\n"; // 3  
//  cout << count_n(array, SIZE, less10); // 3  
// }



// template<typename O> 
// class memfun  
// {  
// public:  
//  memfun(void(O::*f)(const char*), O* o): pFunc(f), pObj(o){}  
//  void operator()(const char* name)  
//  {  
//      (pObj->*pFunc)(name);  
//  }  
// private:  
//  void(O::*pFunc)(const char*);  
//  O* pObj;  
// };

// class A  
// {  
// public:  
//  void doIt(const char* name)  
//  { 
//      cout << "Hello " << name << "!";
//  }  
// };  


// int main(int argc, char *argv[])
// {
//  A a;  
//  memfun<A> call(&A::doIt, &a); // 保存 a::doIt指針以便調用  
//  call("Kitty"); // 輸出 Hello Kitty!   
// }


引用:http://blog.csdn.net/bonchoix/article/details/8050627
http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章