久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

函數指針的意義是什么?

What is the point of function pointers?(函數指針的意義是什么?)
本文介紹了函數指針的意義是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我看不到函數指針的效用.我想它在某些情況下可能很有用(畢竟它們存在),但我想不出使用函數指針更好或不可避免的情況.

I have trouble seeing the utility of function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer.

你能舉一些很好地使用函數指針的例子嗎(在 C 或 C++ 中)?

Could you give some example of good use of function pointers (in C or C++)?

推薦答案

大多數例子都歸結為回調:你調用一個函數 f()code> 傳遞另一個函數的地址 g(),并且 f() 調用 g() 來執行某些特定任務.如果你把f()的地址傳給h(),那么f()會回調h()代碼> 代替.

Most examples boil down to callbacks: You call a function f() passing the address of another function g(), and f() calls g() for some specific task. If you pass f() the address of h() instead, then f() will call back h() instead.

基本上,這是一種參數化函數的方法:它的某些部分行為沒有硬編碼到f()中,但進入回調函數.調用者可以通過傳遞不同的回調函數使 f() 表現不同.一個經典的例子是來自 C 標準庫的 qsort(),它將排序標準作為一個指向比較函數的指針.

Basically, this is a way to parametrize a function: Some part of its behavior is not hard-coded into f(), but into the callback function. Callers can make f() behave differently by passing different callback functions. A classic is qsort() from the C standard library that takes its sorting criterion as a pointer to a comparison function.

在 C++ 中,這通常使用函數對象(也稱為函子)來完成.這些是重載函數調用運算符的對象,因此您可以像調用函數一樣調用它們.示例:

In C++, this is often done using function objects (also called functors). These are objects that overload the function call operator, so you can call them as if they were a function. Example:

class functor {
  public:
     void operator()(int i) {std::cout << "the answer is: " << i << '
';}
};

functor f;
f(42);

這背后的想法是,與函數指針不同,函數對象不僅可以攜帶算法,還可以攜帶數據:

The idea behind this is that, unlike a function pointer, a function object can carry not only an algorithm, but also data:

class functor {
  public:
     functor(const std::string& prompt) : prompt_(prompt) {}
     void operator()(int i) {std::cout << prompt_ << i << '
';}
  private:
     std::string prompt_;
};

functor f("the answer is: ");
f(42);

另一個優點是有時內聯調用函數對象比通過函數指針調用更容易.這就是為什么在 C++ 中排序有時比在 C 中排序更快的原因.

Another advantage is that it is sometimes easier to inline calls to function objects than calls through function pointers. This is a reason why sorting in C++ is sometimes faster than sorting in C.

這篇關于函數指針的意義是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 国产精品天美传媒入口 | 一区二区视频在线 | 日日干夜夜爽 | 一区二区高清视频 | aaa黄色| 日本福利视频 | 国产草草影院 | 免费看a级片 | 久久av网| 一级毛片黄色 | 成人在线网址 | 国产欧美久久久 | 激情久久久久 | 91午夜精品亚洲一区二区三区 | 欧美一区二区精品 | 精品粉嫩小bbwbbwbbw | www.操| 国产精品一区一区三区 | 亚洲黄色精品 | 日韩中文字幕 | 国产欧美日韩视频 | 日韩天堂av | 亚洲视频在线免费观看 | 91精品国产乱码久久久 | 三级视频在线观看 | 成人午夜影院 | 亚洲精品观看 | 激情91 | 中文字字幕在线 | 日本三级大片 | av黄| 成人av一区二区三区在线观看 | 亚洲天堂欧美 | 精品一区二区三区免费毛片 | 国产伦精品一区二区三区四区免费 | 一级免费黄色片 | 亚洲久久久久久 | 亚洲欧美久久 | 亚洲黄色在线 | 一级理论片 | 黄色一级大片在线免费看国产一 |