[英]What does it mean to have a function pointer type?
I have a class assignment that says: 我有一个课堂作业,上面写着:
Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type.
为带有两个int参数并返回int的函数编写声明,并声明一个向量,其元素具有此函数指针类型。
Since the function and vector are both int is this correct? 既然函数和向量都是int的,这是正确的吗? I'm really fuzzy on pointers still.
我对指针仍然很模糊。 This is what I have:
这就是我所拥有的:
#include <iostream>
#include <vector>
using std::cin; using std::cout; using std::endl; using std::vector;
// This is my function that take two ints and returns and int
int AddFunc ( int a, int b) { int addResult; addResult = a + b; return (addResult);}
int main()
{
vector <int> v1; // Declare a vector whose elements have this functions pointer types
int add1, add2, add3 = 0;
cout << "Enter two numbers to be added with my AddFunc function: ";
cin >> add1 >> add2;
add3 = AddFunc (add1, add2);
cout << "The numbers added equal: " << add3 << endl;
v1.push_back(add3);
cout << "The first element in the vector v1 is: " << v1 [0] << endl;
return 0;
}
The function pointer type is int (*)(int, int)
. 函数指针类型为
int (*)(int, int)
。 You want this: 你要这个:
typedef int (*fptr)(int, int);
std::vector<fptr> v;
Example: 例:
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
v.push_back(&add);
v.push_back(&mul);
And then something like this: 然后是这样的:
for (f : v) { std::cout << "f(5, 7) = " << f(5, 7) << std::endl; }
In C++ you have the abilty to define a function pointer, like so: 在C ++中,您可以定义函数指针,如下所示:
int (*pfunc) (int, int);
This is a variable, to which you can assign the address of a function, as long as it has the specified signature, like so: 这是一个变量,您可以为其分配函数的地址,只要它具有指定的签名即可,如下所示:
pfunc = AddFunc; // Assign the adress of a function
pfunc(1,2) // Now call the function through the pointer
Now, note that pfunc
is the variable name, and its "official" type is int (*) (int,int)
Naturally, this can all get very confusing, so you'd probably wanna typedef it: 现在,请注意
pfunc
是变量名,它的“正式”类型是int (*) (int,int)
自然,这一切都非常令人困惑,因此您可能想对它进行typedef:
typedef int (*AdderFunc)(int, int);
AdderFunc pfunc = AddFunc;
pfunc(1,2);
The assignment wants a vector of function pointers for your function. 该分配需要为您的函数提供函数指针的向量。 Function pointers are a bit difficult to read if you're not used to them.
如果您不习惯使用函数指针,将很难读取它们。 Here's a method you can use to write a function pointer type.
这是您可以用来编写函数指针类型的方法。
First, write the argument list of the function. 首先,编写函数的参数列表。
AddFunc()
takes two int
arguments, so at first you have: AddFunc()
具有两个int
参数,因此首先您需要:
(int, int)
For this to be a function pointer, you prefix it with (*)
: 为了使它成为函数指针,请在其前面加上
(*)
:
(*)(int, int)
Lastly, you prefix the whole thing with the return type of the function. 最后,在整个内容前加上函数的返回类型。 In this case,
int
: 在这种情况下,
int
:
int (*)(int, int)
You can use that as-is as the type of your vector: 您可以按原样将其用作向量的类型:
std::vector<int (*)(int, int)> v;
This will work. 这将起作用。 However, it's usually more clear to actually define your own type for the function pointer (using
typedef
). 但是,通常更实际地为函数指针定义自己的类型(使用
typedef
)。 At first, it seems tricky to do so, but it's easy. 起初,这样做似乎很棘手,但这很容易。 You already wrote down the type using the above method.
您已经使用上述方法写下了类型。 The only thing that's missing is to give it a name.
唯一缺少的是给它起一个名字。 You do that by writing the name after the
*
in (*)
. 您可以通过在
(*)
的*
后面写上名称来实现。 For example (*func_ptr)
, so you have: 例如
(*func_ptr)
,因此您具有:
int (*func_ptr)(int, int)
And that's all you need for a typedef
: 这就是您需要的
typedef
:
typedef int (*func_ptr)(int, int);
Now whenever you need that particular function pointer type, instead of: 现在,只要您需要该特定的函数指针类型,就可以:
int (*)(int, int)
you can write: 你可以写:
func_ptr
instead. 代替。 So your vector declaration becomes:
因此,您的向量声明变为:
std::vector<func_ptr> v;
As far as vector is concerned, func_ptr
is a pointer type just like any other. 就vector而言,
func_ptr
就像其他任何指针一样都是指针类型。
Function pointers differ from "regular" pointers in that "dereferencing" them means calling the function they point to. 函数指针与“常规”指针的不同之处在于,“解引用”它们意味着调用它们指向的函数。 You don't actually dereference a function pointer.
您实际上并没有取消引用函数指针。 Instead, you use the
()
operator on them. 而是对它们使用
()
运算符。 Doing that calls the function they point to. 这样做会调用他们指向的函数。
Assigning to a function pointer means assigning it the address of a function. 分配给函数指针意味着分配给它一个函数的地址。 For example, if you have a variable of type
func_ptr
(which you typedef
ed above): 例如,如果你有类型的变量
func_ptr
(你typedef
上述ED):
func_ptr AddFunc_p;
You can assign it the address of AddFunc()
with: 您可以使用以下命令为其分配
AddFunc()
的地址:
AddFunc_p = AddFunc;
Note the missing parentheses in AddFunc
. 请注意
AddFunc
缺少的括号。 You only write the name of the function, since you don't want to actually call that function, but rather want its address so you can assign it to AddFunc_p
. 您只写函数的名称,因为您不想实际调用该函数,而是想要其地址,因此可以将其分配给
AddFunc_p
。 Now you can call AddFunc()
through AddFunc_p
with: 现在,您可以通过
AddFunc_p
调用AddFunc()
:
AddFunc_p(some_int, another_int);
You should now be able to deduct how to put function pointers in a vector and how to apply the ()
operator on its elements. 现在,您应该能够推断出如何将函数指针放入向量中以及如何将
()
运算符应用于其元素。 It will help to keep in mind that the vector simply contains elements of type func_ptr
. 请记住,向量只包含
func_ptr
类型的func_ptr
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.