[英]Calling a function through a function pointer - dereference the pointer or not? What's the difference?
[英]What's the difference between function with dereference and without dereference
f1,(* f1),f2,(* f2)之間有什么區別? (功能)和(&功能)有什么區別?
#include <iostream>
using namespace std;
void function (char *s) {
cout << s << endl;
}
int main () {
void (*f1) (char*) = &function;
void (*f2) (char*) = function;
f1 ("f1 function without dereference.");
(*f1) ("f1 function with dereference.");
f2 ("f2 function without dereference.");
(*f2) ("f2 function with dereference.");
return 0;
}
f1,(* f1),f2,(* f2)之間有什么區別?
f1
和f2
是函數指針。 (*f1)
和(*f2)
是對函數的引用。 函數指針和函數引用之間有什么區別? 極少,因為它們都可以用完全相同的語法調用。 但是,請參閱此問題以獲得有關函數引用的更深入的解釋。
(功能)和(&功能)有什么區別?
function
是一種功能。 &function
是指向&function
的指針。 這里一個非常小的區別是,您可以將函數引用綁定到函數,但不能綁定到函數指針。
void (&fref1)(char*) = function; // compiles
void (&fref2)(char*) = &function; // does not compile
再次,請查看鏈接的問題,以了解可能使用函數引用的原因(並不多)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.