谈函数指针(全局/类成员函数)和函数对象

 
谈函数指针 ( 全局 / 类成员函数 ) 和函数对象
 
作者: Ackarlix
 
  一. 函数指针类型为全局函数.
以下是引用片段:
  #include "stdafx.h"
  #include 
  using namespace std;
  class TestAction;
  typedef void (*fp)(int);
  void Drink(int i)
  {
  cout<<"No. "< 
  }
  void Eat(int i)
  {
  cout<<"No. "< 
  }
  class TestAction
  {
  public:
  fp testAct;
  void TestAct(int i)
  {
  if (testAct != NULL)
  {
  testAct(i);
  }
  }
  };
  int main(int argc, char* argv[])
  {
  TestAction doact;
  doact.testAct = &Drink;
  doact.TestAct(0);
  doact.TestAct(1);
  doact.TestAct(2);
  doact.testAct = &Eat;
  doact.TestAct(0);
  doact.TestAct(1);
  doact.TestAct(2);
  return 0;
  }
 
 二. 函数指针类型为类成员函数.
以下是引用片段:
  #include "stdafx.h"
  #include 
  using namespace std;
  class Action;
  class TestAction;
  // 函数指针类型为类 Action 的成员函数
  typedef void (Action::*fp)(int);
  class Action
  {
  public:
  void Drink(int i)
  {
  cout<<"No. "< 
  }
  void Eat(int i)
  {
  // 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=1005&d=sc37og
  cout<<"No. "< 
  }
  };
  class TestAction
  {
  public:
  // 定义一个函数指针
  fp testAct;
  //Action 对象实例 , 该指针用于记录被实例化的 Action 对象
  Action * pAction;
  void TestAct(int i)
  {
  if ((pAction != NULL) && (testAct != NULL))
  {
  // 调用

  (pAction->*testAct)(i);
  }
  }
  };
  int main(int argc, char* argv[])
  {
  Action act;
  TestAction doact;
  doact.pAction = &act;
  doact.testAct = Action::Drink;
  doact.TestAct(0);
  doact.TestAct(1);
  doact.TestAct(2);
  doact.testAct = Action::Eat;
  doact.TestAct(0);
  doact.TestAct(1);
  doact.TestAct(2);
  return 0;
  }
 
 三. 函数对象 (Function object)
以下是引用片段:
  #include "stdafx.h"
  #include 
  #include 
  using namespace std;
  class Action;
  class Drink;
  class Eat;
  class TestAction;
  class Action
  {
  public:
  int operator()(int i)
  {
  Act(i);
  return i;
  }
  virtual void Act(int i) = 0;
  };
  class Drink : public Action
  {
  void Act(int i)
  {
  cout<<"No. "< 
  }
  };
  class Eat : public Action
  {
  void Act(int i)
  {
  cout<<"No. "< 
  }
  };
  class TestAction
  {
  public:
  void TestAct(int i, Action& testAct)
  {
  testAct(i);
  }
  };
  int main(int argc, char* argv[])
  {
  TestAction doact;
  doact.TestAct(0, Drink());
  doact.TestAct(1, Drink());
  doact.TestAct(2, Drink());
  doact.TestAct(0, Eat());
  doact.TestAct(1, Eat());
  doact.TestAct(2, Eat());
  return 0;
  }
 
  虽然传递函数指针被广泛应用于事件驱动系统中,以此实现回调函数通过指针来调用。但 C++ 还是提供了另外一种可供选择的办法,即函数对象,利用它可以避免使用函数指针。这样做有几个优点。首先, 因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结果的数据成员。。 此外,编译器可以内联函数对象,从而进一步增强性能。函数对象可以具体表达依赖成员模板的通用算法 , 这些算法借助普通的函数指针难以完成。例用函数对象实现了一个通用的 Negation 算法操作:
以下是引用片段:
  #include "stdafx.h"
  #include 
  using namespace std;
  class Negate
  {
  public:
  template T operator()(T t) const
  {
  return -t;
  }
  };
  void Callback(int n, const Negate& neg) // 传递一个函数对象
  {
  n = neg(n); // 调用重载的 () 操作 来对 n 进行 negate 操作
  cout << n << endl;
  }
  int main(int argc, char* argv[])
  {
  // 调用方式一
  Callback(5, Negate());
  // 调用方式二
  Negate neg;
  cout << neg(9.99999) << endl;
  cout << neg(__int32(39999999)) << endl;
  return 0;
  }
  STL 库中定义了很多函数对象以供相关算法调用,如 模板化的函数对象 greater<> 或者 less<>:
  vector vi;
  //.. 填充向量
  sort(vi.begin(), vi.end(), greater() );// 降序 (descending)
  sort(vi.begin(), vi.end(), less() ); // 升序 (ascending)
 
 
本文来源:http://blog.csdn.net/ackarlix/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值