[英]Returning function pointer type
我經常發現需要編寫返回函數指針的函數。 每當我這樣做時,我使用的基本格式是:
typedef int (*function_type)(int,int);
function_type getFunc()
{
function_type test;
test /* = ...*/;
return test;
}
但是,在處理大量函數時這會變得很麻煩,所以我不想為每個函數(或每個函數類)聲明一個 typedef
我可以刪除 typedef 並將函數中返回的局部變量聲明為: int (*test)(int a, int b);
使函數體看起來像這樣:
{
int (*test)(int a, int b);
test /* = ...*/;
return test;
}
但后來我不知道為函數的返回類型設置什么。 我努力了:
int(*)(int,int) getFunc()
{
int (*test)(int a, int b);
test /* = ...*/;
return test;
}
但這會報告語法錯誤。 如何在不為函數指針聲明 typedef 的情況下聲明此類函數的返回類型。 甚至可能嗎? 另請注意,我知道為每個函數聲明 typedef 似乎會更干凈,但是,我非常小心地將我的代碼構造為盡可能干凈和易於遵循。 我想消除 typedef 的原因是它們通常只用於聲明檢索函數,因此在代碼中似乎是多余的。
int (*getFunc())(int, int) { … }
這提供了您要求的聲明。 此外,正如ola1olsson所說,最好插入void
:
int (*getFunc(void))(int, int) { … }
這表示getFunc
可能不帶任何參數,這有助於避免錯誤,例如有人無意中編寫getFunc(x, y)
而不是getFunc()(x, y)
。
您可能可以執行以下操作:
int foo (char i) {return i*2;}
int (*return_foo()) (char c)
{
return foo;
}
但是上帝,我希望我永遠不必調試你的代碼......
我把它留在這里,因為它比已經給出的答案有點棘手,因為它需要一個函數指針
(int (__cdecl *)(const char *))
並返回一個函數指針
(int (__cdecl *)(const char *))
#include <stdio.h>
int (*idputs(int (*puts)(const char *)))(const char *) {
return puts;
}
int main(int argc, char **argv)
{
idputs(puts)("Hey!");
return 0;
}
在將一些 C 代碼包裝在 C++ 類中時,我有與原始海報相同的願望:從函數返回函數指針,而無需求助於typedef
函數指針原型。 我遇到了一個我認為值得分享的 C++ const
正確性問題,即使它有點離題(C++),但它確實與原始問題直接相關:返回 C 函數指針而不求助於typedef
的語法。
下面的代碼定義了一個A
類,它存儲一個函數指針並通過get_f()
調用將其暴露給外部世界。 這是應該返回沒有typedef
的函數指針的函數。
重點(讓我難過一段時間)是如何聲明get_f()
是一個const
函數,即它不會改變A
。
該代碼包含 2 個變體:第一個使用 typedef 作為函數指針原型,而第二個將所有內容全部寫出。 #if
在兩者之間切換。
#include <iostream>
int my_f(int i)
{
return i + 1;
}
#if 0 // The version using a typedef'ed function pointer
typedef int (*func_t)(int);
class A
{
public:
A(func_t f) : m_f(f) {}
func_t get_f() const { return m_f; }
private:
func_t m_f;
};
int main(int argc, char *argv[])
{
const A a(my_f);
std::cout << "result = " << a.get_f()(2) << std::endl;
}
#else // The version using explicitly prototyped function pointer
class A
{
public:
A(int (*f)(int)) : m_f(f) {}
int (*get_f() const)(int) { return m_f; }
private:
int (*m_f)(int);
};
int main(int argc, char *argv[])
{
const A a(my_f);
std::cout << "result = " << a.get_f()(2) << std::endl;
}
#endif
預期/期望的輸出是:
result = 3
關鍵是const
限定符在該行中的位置:
int (*get_f() const)(int) { return m_f; }
這是一個愚蠢的例子,但它很簡單並且不會出錯。 這只是關於聲明靜態函數:
#include <stdio.h>
#include <stdlib.h>
void * asdf(int);
static int * hjkl(char,float);
main() {
int a = 0;
asdf(a);
}
void * asdf(int a) {return (void *)hjkl; }
static int * hjkl(char a, float b) {int * c; return c;}
我認為你有三個選擇:
您可以編寫以下代碼(僅適用於 C++11 及更高版本):
//C++11
auto func(...) {
int (*fptr)(...) ret = ...
//Do sth.
return ret;//C++11 compiler will automatically deduce the return type for you
}
或者,如果你不喜歡自動推導返回類型,可以在函數末尾指定類型(同上,只在C++11及以上):
//C++11
auto func(...) -> int (*)(...) { /* Do sth. */ }
以 void 作為參數的簡單示例 fun 函數,它返回函數指針,該函數應采用 int* 和 int* 並返回 int*
使用 typedef 檢查鏈接示例https://www.geeksforgeeks.org/returning-a-function-pointer-from-a-function-in-c-cpp/
#include <stdio.h>
int* fun1(int* y, int* z)
{
static int var = 0;
var = (*y + *z);
printf("Inside fun1 = %d\n", var );
return &var;
}
int* (*fun())(int*,int*)
{
printf("Inside fun\n");
return &fun1;
}
int main()
{
int a=10,b=20;
int *ptr = NULL;
int* (*(*fp)())(int*,int*) = &fun;
ptr=(*fp())(&a,&b);
printf("*ptr = %d\n", *ptr );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.