C++ 参数中没有指针的函数指针

C++ 参数中没有指针的函数指针,c++,function,pointers,C++,Function,Pointers,我有这次考试复习,其中一个问题是: 为函数void myfun(int yourage)编写一个函数点[er] 我不确定如何在不向参数添加任何内容的情况下使用该函数。。我了解函数指针的基本知识,并提出了一个非常基本的场景,我认为这可以解决问题,即: void myfun(int (*fptr)(int), int yourage) { cout << fptr(yourage) << endl; } int yourage(int x) { return

我有这次考试复习,其中一个问题是: 为函数void myfun(int yourage)编写一个函数点[er]

我不确定如何在不向参数添加任何内容的情况下使用该函数。。我了解函数指针的基本知识,并提出了一个非常基本的场景,我认为这可以解决问题,即:

void myfun(int (*fptr)(int), int yourage)
{
    cout << fptr(yourage) << endl;
}
int yourage(int x)
{
    return x; //really simple
}

int main()
{
    int age = 10;
    int (*pfnc)(int);
    pfnc = yourage;
    myfun(pfnc,age);
    return 0;
}
void myfun(int(*fptr)(int),int yourage)
{
库特

#包括
typedef void(*pFunc)(int);//声明函数指针类型
void my_function(int v)//要指向的函数
{

如果原始函数为:

void myfun(int yourage)
{
    cout << "age is: " << yourage << endl;
}

它是否按预期工作?“我不必更改原始函数?”您更改了哪个原始函数?如果您问我,我会称之为“为函数
yourage
”生成函数指针“@πάντα”ῥεῖ 这个函数可以工作,但我认为它不是教授想要的格式。原始函数是void myfun(int yourage);@Jeremie至于要求
为函数void myfun(int yourage);
对我来说看起来很好(尽管命名有点混乱)…您是否获得了
myfun
的原始定义?显示它。这并不能回答问题,尽管我甚至不确定问题是什么。@JesseGood“我不确定如何在不向参数添加任何内容的情况下使用该函数。”-这给我的印象是他不知道如何声明和使用函数指针。@ZacHowland OP提到熟悉函数指针…@πάνταῥεῖ 然后去表明他不是。@Zac:怎么会这样?他似乎不懂指令,但他在问题中使用的函数指针看起来很完美。
#include <iostream>

typedef void (*pFunc)(int); // declare the function pointer type

void my_function(int v) // the function you want to point to
{
    std::cout << "value = " << v << std::endl;  
}

void func(pFunc f, int v) // a function that takes a function pointer as a paramter
{
    f(v);
}

int main() 
{
    pFunc myFunc = &my_function;
    func(myFunc, 5); // call the function with a function pointer parameter
    return 0;
}
void myfun(int yourage)
{
    cout << "age is: " << yourage << endl;
}
int main()
{
    // write a function pointer for myfun
    typedef void myfun_type(int);
    myfun_type* myfun_ptr = &myfun;

    //now use it
    int a[] = { 31, 27, 25, 23, 21, 18 };
    for_each(begin(a), end(a), myfun_ptr);
}