gpt4 book ai didi

c++ - 创建一个以函数指针作为参数的函数指针

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:21 25 4
gpt4 key购买 nike

如何创建一个以函数指针为参数的函数指针(c++)???我有这个代码

    #include <iostream>
using namespace std;

int kvadrat (int a)
{
return a*a;
}
int kub (int a)
{
return a*a*a;
}
void centralna (int a, int (*pokfunk) (int))
{
int rezultat=(*pokfunk) (a);
cout<<rezultat<<endl;

}

void main ()
{
int a;
cout<<"unesite broj"<<endl;
cin>>a;
int (*pokfunk) (int) = 0;
if (a<10)
pokfunk=&kub;
if (a>=10)
pokfunk=&kvadrat;

void (*cent) (int, int*)=&centralna; // here i have to create a pointer to the function "centralna" and call the function by its pointer

system ("pause");
}

最佳答案

你会发现 typedef 函数指针更容易。

typedef int (*PokFunc)(int);
typedef void (*CentralnaFunc)(int, PokFunc);
...
CentralnaFunc cf = &centralna;

关于c++ - 创建一个以函数指针作为参数的函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3199494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com