国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

談函數指針(全局/類成員函數)和函數對象

2019-11-17 05:02:52
字體:
來源:轉載
供稿:網友
  函數指針(全局函數/類成員函數)、函數對象(Function object)  一. 函數指針類型為全局函數.以下是引用片段:
  #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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长海县| 奎屯市| 绥化市| 靖州| 喜德县| 滦南县| 云阳县| 赤城县| 山东省| 马尔康县| 洛南县| 互助| 辽宁省| 峨眉山市| 贵定县| 札达县| 犍为县| 桑植县| 米泉市| 民和| 大庆市| 开江县| 长白| 滕州市| 古浪县| 武乡县| 灵宝市| 兴城市| 鄢陵县| 抚远县| 岫岩| 图木舒克市| 漳州市| 东宁县| 岑溪市| 五原县| 新乡县| 平山县| 平湖市| 沙洋县| 得荣县|